#Can this be considered a TypeScript issue? Record number keys and number template literal as key

8 messages · Page 1 of 1 (latest)

ocean flowerBOT
#
morcojel#0

Preview:```ts
type R = Record<number, string>

type T = ${number}

const t1: T = ".2"
const t2 = ".2" as const

const r: R = {
[t1]: "abc",
[t2]: "abc",
}```

burnt oak
#

this doesn't really help, but it's not an issue specific to template literals btw, here's an example with a plain old string

ocean flowerBOT
#
littlelily#0

Preview:```ts
const x: string = "asdf"

const r: Record<number, string> = {
[x]: "def",
}```

burnt oak
#

I think the most likely explanation is something to do with the symbol type and how passing a variable name between [ ] on an object treats the text between the brackets as a symbol and not whatever type it was originally

#

cause it only occurs when it's a variable name between the [ ]

ocean flowerBOT
#
littlelily#0

Preview:```ts
const x: string = "abc"

const r: Record<number, string> = {
[x]: "foo",
["def"]: "bar",
}

const s: Record<number, string> = {
[x]: "foo",
def: "bar",
}```

deep compass
#

passing a variable name between [ ] on an object treats the text between the brackets as a symbol and not whatever type it was originally
that's not correct (at least if you're talking about the JS primitive named symbol). { ['a']: 1 } and { a: 1 } produce the same object at runtime

#

[ ]: (computed property names) just lets you write expressions to specify keys rather than being limited to bare identifiers or strings