#How can I change this code to make "theNumber" be of type numer and "theString" be of type string?

3 messages · Page 1 of 1 (latest)

remote boneBOT
#
hornta#4659

Preview:```ts
const myNum = 123
const myString = "abc"

const myVars = {
nums: [myNum],
strs: [myString],
}

type MyVarsKey = keyof typeof myVars

const func = <K extends keyof typeof myVars>(
key: K,
index: number
) => {
return myVars[key][index]
}

const theNumber = func("nums", 0)
...```

craggy escarp
#

@lament mason Instead of a cast, an annotation is safer