【发布时间】:2021-01-18 11:57:07
【问题描述】:
我需要确保bar的值必须是只读对象FOO的键,除了键c。
代码
const FOO = {
a: {
// key-value pairs
},
b: {
// key-value pairs
},
c: {
// key-value pairs
},
} as const;
// all of the keys are assignable
const allKeys: keyof typeof FOO = 'a';
// tried this but TypeScript doesn’t throw error
const bar: Omit<keyof typeof FOO, 'c'> = 'c';
【问题讨论】:
-
请不要对每个问题提出多个问题,发布一个新问题(如果相关,请提供指向该问题的链接):)
-
当然,明白了:)
标签: javascript reactjs typescript object object-literal