首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么TypeScript在将类分配给接受比类多的类的接口时没有抱怨呢?

为什么TypeScript在将类分配给接受比类多的类的接口时没有抱怨呢?
EN

Stack Overflow用户
提问于 2022-03-09 23:07:39
回答 1查看 65关注 0票数 5

当我将类分配给接口时,为什么TypeScript不抱怨,其中接口是类的超集。

示例:

代码语言:javascript
复制
interface AI {
  hello(msg: string | number): void;
}

class A {
  hello(msg: string) {}
}

const a = new A();

const b: AI = new A(); // Why not complain here?

a.hello(1); // Argument of type 'number' is not assignable to parameter of type 'string'.
b.hello(1);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-10 02:32:08

在TypeScript中,方法总是被选中双变,这意味着允许您扩展它们的参数类型,这是安全的,或者缩小了它们的参数类型,这是不安全的,如下所示:

代码语言:javascript
复制
interface Iface {
  method(msg: string | number): void; // method syntax
}

const obj: Iface = {
  method(msg: string) { msg.toUpperCase() } // okay?!
}

obj.method(1); // runtime error

在本例中,Iface有一个需要接受string | numbermethod(),但是obj (作为Iface注解 )有一个只接受stringmethod()。编译器不会对此发出警告,您将得到一个运行时错误。这似乎正是TypeScript应该避免的事情,但是(根据文献资料),在实践中,人们很少使用双变量方法来做这样不安全的事情。相反,人们倾向于这样做:

代码语言:javascript
复制
class Base {
  constructor(public name: string) { }
  compare(other: Base) {
    return this.name.localeCompare(other.name)
  }
}

class Subclass extends Base {
  constructor(name: string, public age: number) { super(name); }
  compare(other: Subclass) {
    return (this.age - other.age) || this.name.localeCompare(other.name)
  }
}

Subclasscompare()方法在BaseClasscompare()方法接受任何Base时只接受Subclass在技术上是不安全的。但是做这种事情是非常有用和常见的,而且只要不把Subclass向上推到Base,你就不会直接观察到安全漏洞。

无论如何,如果您对防止这种情况感兴趣,可以打开编译器选项 (编译器选项套件的一部分)和重构,以便将任何违规的方法声明重写为函数值属性声明:

代码语言:javascript
复制
interface Iface {
  method: (msg: string | number) => void; // function-valued prop syntax
}

const obj: Iface = {
  method(msg: string) { msg.toUpperCase() } // error!
  //~~~~ <-- Type 'string | number' is not assignable to type 'string'.
}

现在,如果不安全地缩小实现中的方法参数,您将得到警告。注意,您仍然可以通过方法语法实现该方法;objmethod属性不是箭头函数,您仍然可以得到所需的错误。

操场链接到代码

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71417022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档