#Destructuring variable with never type
27 messages · Page 1 of 1 (latest)
no, it's not possible
type never is an impossible type
you should never have it inside of your code for one of your variable
e.g. variable foo can be either a string or a number
if you are doing some checks and its is neither a string or a number, then there is no to other possibility/value left
so that means it doesn't have any attribute, and you can destructure it
@stark lagoon
but why?
Yeah, I'm not sure there's a real satisfying answer here.
In theory you could argue that value.a should be okay - never is essentially supposed to be 'read-only any"... but it's probably just considered more useful to error than to not error.
why it doesn't throw an error then? https://www.typescriptlang.org/play?#code/MYewdgzgLgBAbgQwDYFcCmAuGY1zQJxgF4YBvGAMxBCwEYYBfAbgChFU0A6KkJmAen4wACvhAAHAlACeMAOQ85MACYg0EbCFhoAHgEtoMcDBmT5OPPjmcWoSLHI9GxeMnR9BlajAMnpZiwIAGhU1CDA5WCgACzEAdxgCMXwWFiA
I have an type:
declare function useParams<T extends Record<string, string> = never>(): T;
and I set default type as never, to prevent accessing value from useParams, without adding generic to it, but it's still possible to destructure from the hook:
const { id } = useParams(); // should throw error
const { id } = useParams<{ id: string }>() // should work fine
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@stark lagoon Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:ts const value: never = {foo: 1} value.foo // Property 'foo' does not exist on type 'never'. const {foo} = value // foo is type never, doesn't throw error
this is a known bug
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
1 workaround would be to use a private symbol as a key
then you can't destructure anything from it if you dont provide a generic type argument
feels like you could just avoid the situation altogether. hard to say without seeing your actual use case
it's supposed to be a react hook
yeah but foo is type never, not usable anywhere
so you won't get the error where you want it, but you will see it
ideally errors should occur at the source of the problem, not where the problematic result is being used
Well I agree, but personally don't think a workaround is necessary
thats the use case, the goal here is to force adding generic to useParams, maybe some other type as default would be more suitable than 'never'?
https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtX2QGcQAFWKAW0IB4AVeEADwxFWEPgCVwcZhrCGGFlQBzADTxBwsQD54AXnioQANxAxZACgCUALni0A3AFgAUObB5B8AN7wsweAF9FBYmRiVCu0xbNAA
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@stark lagoon Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
declare function useParams<
T extends Record<string, string> = never
(): T
const {id} = useParams()```