打字稿推断直接推断值并删除密钥是否有可能
Is that possible that typescript inference to direct inference value and drop the key
class A {
state: B
}
class B {
something: C
}
class C {
a: string;
b: boolean;
}
type MagicType = ...
const c: MagicType<A>
c.state.a = "123"
c.state.b = true;
如示例所述,在不更改 class 结构的情况下,是否可以使用魔术类型来实现此目的?
忽略 something
键,只导出 class c
属性
谢谢!
有对应的映射类型。
class Foo{
constructor(public bar:string,public baz:number){}
}
type State<T> = {
"state": { [P in keyof T]: T[P] };
};
var fooState: State<Foo> = {
state : new Foo("bar",1)
}
console.log(fooState.state.bar);
游乐场link
另见 mapped types
class A {
state: B
}
class B {
something: C
}
class C {
a: string;
b: boolean;
}
type MagicType = ...
const c: MagicType<A>
c.state.a = "123"
c.state.b = true;
如示例所述,在不更改 class 结构的情况下,是否可以使用魔术类型来实现此目的?
忽略 something
键,只导出 class c
属性
谢谢!
有对应的映射类型。
class Foo{
constructor(public bar:string,public baz:number){}
}
type State<T> = {
"state": { [P in keyof T]: T[P] };
};
var fooState: State<Foo> = {
state : new Foo("bar",1)
}
console.log(fooState.state.bar);
游乐场link
另见 mapped types