有没有办法用另一个变量的类型来声明一个变量?例如,我声明一个具有某种类型的类成员,然后我想在同一类型的函数中声明另一个变量。但我不想修改原来的声明,也不想重复它。看起来你应该能够做类似的事情:
class Foo {
bar: {[key: string]: string[]};
func() {
const x: TypeOf<Foo.bar> = {};
....
}
}
我听说过类似的专门针对函数返回类型的东西,但我找不到了......
您可以使用typeof
但在课堂上你应该接触到财产:
class Foo {
bar: {[key: string]: string[]};
func() {
const x: typeof Foo.prototype.bar = {};
// here x has type `{[key: string]: string[]}`
}
}
还有一个课外的例子:
class A {
b: string = ''
}
type test = typeof A.prototype.b // type is `string`
操场 https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAECC0G8BQ1XQEYC5oQC4CcBLAOwHNoBeaAcmqQF8klcBPABwFNpcO9Lv2HAPYAzOADo2+IbhmDx6JqEgwAYkKGIUadGHzYEAbQDWHFtjxEyAXQsESpQ9foBuJmmgiArsWAAKAEotDw9gIWI+AA9sVk5RaHUhSWlZWI4FPX4EV21URnogA
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)