假设我有一个对象:
const config = {
initial: 'foo', // must be key of .states
states: {
foo: {},
bar: {}
}
}
我应该如何创建类型定义,以便TypeScript可以断言config.initial
是config.states
的一个键?例如:
type Config = {
initial?: <key of .states>,
states: {
[K: string]: Config
}
}
function createSomething(config: Config) {
// ...
}
// should NOT compile
createSomething({
initial: 'fake',
states: {
foo: { states: {} },
bar: { states: {} }
}
});
我的想法是创建一个强类型配置对象,该对象的属性在createSomething
函数中彼此依赖。这是可能的吗?
Conf
类型应该指的是什么? - ethane