Typescript通用对象属性常量名称在映射后丢失
有没有一种不用“as”的方法Typescript通用对象属性常量名称在映射后丢失,typescript,Typescript,有没有一种不用“as”的方法 const names = ['a' , 'b'] as const // readonly ["a", "b"] type Name = typeof names[number] // "a" | "b" const arr = names.map(n => { // n is of type: "a" | "b" r
const names = ['a' , 'b'] as const // readonly ["a", "b"]
type Name = typeof names[number] // "a" | "b"
const arr = names.map(n => {
// n is of type: "a" | "b"
return {[n]: 'smth'}
})
/*
Array is of type:
{
[x: string]: string;
}[]
How to make it of type:
{
[x in Name]: string;
}[]
*/