简体   繁体   English

Typescript function 类型不执行与返回类型相同的严格性

[英]Typescript function type not enforcing the same strictness as return type

I am trying to define a function type with a return type (Object) to enforce functions implementing it to return the exact object properties defined in the return type.我正在尝试使用返回类型(对象)定义 function 类型,以强制实现它的函数返回返回类型中定义的确切 object 属性。

However the compiler is not being strict about the returned object and allows extra properties that are not defined in the returned object type.但是,编译器对返回的 object 并不严格,并允许在返回的 object 类型中未定义的额外属性。

interface Obj {
  foo: string;
}

type Func = () => Obj;

const fn: Func = () => {
  return {
    foo: 'bar',
    blah: '', // compiler does not show an error
  };
};

Example in TS playground TS 操场中的示例

In contrast, if the return type Obj is specified in the fn function, the compiler shows an error.相反,如果在fn function 中指定了返回类型Obj ,则编译器会显示错误。

interface Obj {
  foo: string;
}

const fn = (): Obj => {
  return {
    foo: 'bar',
    blah: '', // compiler shows an error
  };
};

Example in TS playground TS 操场中的示例

Can somebody explain why TS handles these cases differently?有人可以解释为什么 TS 以不同的方式处理这些案件吗? Is there a way to have the return type strictness while using a function type?有没有办法在使用 function 类型时具有返回类型严格性?

In the first example, the return type of the anonymous function is defined dynamically as follow: { foo: string; blah: string; }在第一个示例中,匿名{ foo: string; blah: string; }的返回类型动态定义如下: { foo: string; blah: string; } { foo: string; blah: string; } . { foo: string; blah: string; } This type is compatible with the type Obj as it has all the required properties of Obj (and more, but that's enough to be considered compatible , as "structural typing is a way of relating types based solely on their members", from https://www.typescriptlang.org/docs/handbook/type-compatibility.html ).此类型与Obj类型兼容,因为它具有Obj的所有必需属性(以及更多,但这足以被视为兼容,因为“结构类型是一种仅基于其成员关联类型的方式”,来自https:/ /www.typescriptlang.org/docs/handbook/type-compatibility.html )。

In the end, the Func type is compatible with the silently defined type of the anonymous function () => { foo: string; blah: string; }最后, Func类型兼容匿名 function () => { foo: string; blah: string; } () => { foo: string; blah: string; } () => { foo: string; blah: string; } , so an "implicit conversion" takes place and it compiles. () => { foo: string; blah: string; } ,因此会发生“隐式转换”并进行编译。

In the second example, the signature enforces the return type of the anonymous function itself, so the returned object is a literal that's supposed to be exactly of type Obj .在第二个示例中,签名强制执行匿名 function 本身的返回类型,因此返回的 object 是一个应该完全属于Obj类型的文字。 Thus, for the same reason you can't do: let obj: Obj = { foo:'bar', blah:'' } , this code can't compile because there's no compatibility check in this case, as its type is explicitly defined.因此,出于同样的原因,您不能这样做: let obj: Obj = { foo:'bar', blah:'' } ,此代码无法编译,因为在这种情况下没有兼容性检查,因为它的类型是明确的定义。

Cheers!干杯!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM