Is it possible to use `keyof` operator on literals instead of interfaces?

前端 未结 2 1261
春和景丽
春和景丽 2021-02-01 12:00

I have an object literal such as the following (all properties are known at compile time):

const foo = {
  \"hello\": \"hola\"
};

If foo<

2条回答
  •  被撕碎了的回忆
    2021-02-01 12:43

    a bit off, while i'm finding indexer key to literals, but put it here for future reference.

    const foo = {
      "hello": "hola"
    };
    
    let data: { [key in keyof typeof foo]:number} & { name: string, index: number }[] = [] as any;
    
    data.foo = 1;
    data[0] = {name:'foo', 1};
    

提交回复
热议问题