Hey, wizards. Any clue why I can't use index access, even though I've checked that the key exists on the object?
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
6 messages · Page 1 of 1 (latest)
Hey, wizards. Any clue why I can't use index access, even though I've checked that the key exists on the object?
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
in only narrows the right hand side (the object), not the left hand side (the key). if you hover over path inside the if statement you'll see its type is exactly the same
maybe you would benefit from a library like express or fastify (my favourite is zodios) that handles creating api definitions, parsing query params from string literal paths like "/foo/:bar", and a bunch of other stuff that helps with type safety
that's not to say you cant make your own way of doing it, it's just not the simplest thing to implement
something like this might be a better way to structure things if you want to implement it yourself: