#Strange behaviour of enums, node server, react, vscode and git.

10 messages · Page 1 of 1 (latest)

pseudo surge
#

const navItem = Object.values(Path).filter(item => pathname.includes(item))

Path is an enum with string values.
I was getting error on the item saying Arg of type unknown is not assignable to param of type 'string'.

This compilation error did not exist before. I did not change anything regarding the code. It seems to have popped up after I changed to an old branch and changed back.
I then wrote some code (to check some types const x = typeof Object.values(Path) sorta useless code btw) and then commented it out, effectively changing nothing, and somehow it worked again.

I tried restarting the ts server earlier nothing happened. It seems like something to do with enums, the node server thats running my react app and changing branches that may have added untracked files or changed file casing that also isn't tracked...
(i.e I have run into compilation error after switching to and from an old branch where I changed casing of a file, because git doesn't track those changes for some reason. - fixed by just changing the file back to my wanted casing)

Note: This error was on the dev server (as well as visualised on chrome - showing compilation error)
Note2: I'm using VSCode - there was no red squiggly line under the item but in the chrome browser it does show.

Any feedback/ideas as to the cause of this error would be welcome!

marble cargo
#

Make a demo?

fresh masonBOT
#
sandiford#0

Preview:```ts
const pathname = "my/path"

enum Path {
foo,
bar,
}

const navItem = Object.values(Path).filter(item => {
item
// ^?
console.log(item)
pathname.includes(item)
})

type Equal<A, B> = [A, B] extends [B, A] ? true : false

type PathTypeIs12 = Equal<Path, 1 | 2>```

marble cargo
#

I would not encourage using enums like that

#

Path[Path["foo"] = 0] = "foo";
Path[Path["bar"] = 1] = "bar";

#

extract this out and the enum is

Path = {
    foo: 1,
    bar: 2,
    1: foo,
    2: bar
}
#

enum is a rare situation where a type emits some actually javascript. But to go ahead and iterate over it as data is not somewhere I would go

fresh masonBOT
#
sandiford#0

Preview:```ts
const paths = ["foo", "bar"] as const
type Path = typeof paths[number]
// ^?

for (const path of paths) {
path
// ^?
}

function withPath(path: Path) {}

withPath("foo")
withPath("baz") // Argument of type '"baz"' is not assignable to parameter of type '"foo" | "bar"'.(2345)```

marble cargo
#

Something like this might be a better structure. Depends exactly what you are doing