#Typescript won't allow me to assert a type?

8 messages · Page 1 of 1 (latest)

elfin osprey
#

I have a type that can't be determined ahead of usage. Now I know it looks like [string, Record<string, string>] but for some reason typescript won't let assert the value as that type.

const foo = bar as unknown as [string, Record<string, string>]; // foo is still given as [string, any]??
pale rainBOT
#
evan.trimboli#0

Preview:ts const foo = {} as unknown as [ string, Record<string, string> ] // foo is still given as [string, any]??

stray gorge
#

No it still shows Record in the playground

thorn dirge
#

yeah, looks right to me:

pale rainBOT
#
const foo = {} as unknown as [string, Record<string, string>]; // foo is still given as [string, any]??
//    ^? - const foo: [string, Record<string, string>]

const bar = foo[1]['test']
//    ^? - const bar: string
thorn dirge
#

@elfin osprey if you're using VSCode maybe try restarting tsserver? otherwise if you can share more details about your setup that'd be great

elfin osprey
#

using nvim with ts lsp - restarted lsp multiple times to no avail and resorted to // @ts-ignore for that one variable just to retype it one line below. Later on I've moved the function containing bar out of the scope where the type in question was inferred and there it works as intended

thorn dirge
#

that's bizarre. out of curiosity, do you know what language server you're using? i mostly use VSCode which directly uses tsserver (which IIUC isn't quite an LSP server). maybe this one?