Typescript忽略类类型定义上的修饰mobx属性
这是一个简化的例子。我有一个使用装饰器的类定义,如:Typescript忽略类类型定义上的修饰mobx属性,typescript,webpack,typescript-typings,mobx,Typescript,Webpack,Typescript Typings,Mobx,这是一个简化的例子。我有一个使用装饰器的类定义,如: export default class AnimationController { @observable animationTime: number = 0; @computed({keepAlive: true}) get interpolatedJoints(){} @computed({keepAlive: true}) get currentAnimation(){} } 在其他地方,我导入这个类作为类型定义使用,并
export default class AnimationController {
@observable animationTime: number = 0;
@computed({keepAlive: true}) get interpolatedJoints(){}
@computed({keepAlive: true}) get currentAnimation(){}
}
在其他地方,我导入这个类作为类型定义使用,并在访问这些预期属性时出错
import AnimationController from './AnimationController';
type Animate = {
controllers: typeof AnimationController[];
};
//...
if(
(this.controllers[0].currentAnimation.name === 'slice' || this.controllers[0].currentAnimation.name === 'punch')
&& this.controllers[0].interpolatedJoints.currentAnimationInfo.lowerKeyframeNumber > 1
&& this.controllers[0].interpolatedJoints.currentAnimationInfo.lowerKeyframeNumber < 4
){
我的tsconfig.json如下所示
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2019",
"dom"
]
}
}
您不需要在此处输入
typeof
:
type Animate = {
controllers: typeof AnimationController[];
// Should be:
controllers: AnimationController[];
};
typeof AnimationController
表示您想要的是类构造函数,而不是类实例。但据我所知,实际上您有一个类实例数组
typeof
可用于以下情况:
class Foo {
// some code here
}
function createClass(klass: typeof Foo) {
return new klass();
}
const newInstanceOfFoo = createClass(Foo);
谢谢这似乎就是问题所在!
class Foo {
// some code here
}
function createClass(klass: typeof Foo) {
return new klass();
}
const newInstanceOfFoo = createClass(Foo);