例如,我有一些界面:
interface Foo {
bar: string;
baz: (param: any) => void;
}
和一个对象:
const test = {
bar: 'id',
baz: (param) => console.log(param.id),
};
在不使用映射类型的情况下,如何强制TypeScrip将bar
值用作param
的key
类型?所以我想要像这样的东西:
interface Foo<J> {
bar: J;
baz: (param: {[key: J]: any}) => void;
}
和一个对象:
const test = {
bar: 'id',
baz: ({id}) => console.log(id), // <== here should typescript allow only the id property
};
我知道我可以使用映射类型来实现该行为,但这不是我在这里想要的