简体   繁体   English

类实现接口,但是成员变量方法的参数没有类型?

[英]Class implements interface, but the arguments to a method of a member variable have no type?

So let's just look at my code. 因此,让我们看一下我的代码。 It's not very well described. 描述不是很好。

This is interface: 这是接口:

interface IModel<T = any> {
  effects: {
    [key: string]: (getState: () => T) => void;
  };
}

interface IState {
  name: string;
  age: number;
}

This is wrong, but I don't know why: 这是错误的,但我不知道为什么:


class Model implements IModel<IState> {
  effects = {
    getName: (getState) => {
      const { /** Here, Ts has no smart tips */ } = getState();
    }
  };
}

Need me to add type, just can have tips: 需要我添加类型,只需提示即可:


class Model implements IModel<IState> {
  effects = {
    getName: (getState: () => IState) => { // add type
      const { /** Yes, have 'name' and 'age' */ } = getState();
    }
  };
}

But, I think the latter approach is a bit verbose. 但是,我认为后一种方法有点冗长。

Unfortunately you can't do this. 不幸的是你不能这样做。 Check https://github.com/Microsoft/TypeScript/issues/1373 for updates. 检查https://github.com/Microsoft/TypeScript/issues/1373以获取更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript:实现某个接口或方法的 class 的类型 - TypeScript: type of class that implements a certain interface or method 接口字段应具有实现该接口的类的类型-typescript - Interface field should have type of the class that implements that interface - typescript 如何确保Class的参数类型实现接口 - How to ensure that argument type of Class implements an interface 如何推断实现接口的 class 类型 - How to inference class type that implements an interface 如何返回接口中方法的 ReturnType 元组,这是一种可变参数类型 - How to return a tuple of ReturnType of a method in the interface which is a type of variable arguments 如何在课堂上实现接口? - How to implements interface on class? Typescript - 使函数返回类型成为接口实现的类 - Typescript - Make the function return type the class that an interface implements 实现接口的类的类型中不存在属性,编译器错误 - Property does not exist in the type of class which implements an interface, compiler error typescript类实现签名不正确的接口方法时没有错误 - No error when typescript class implements interface method with incorrect signature TypeScript Class 使用联合类型字段实现接口导致错误 (2322) - TypeScript Class Implements Interface with Union Type Field results in Error (2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM