简体   繁体   English

TypeScript:没有显式返回类型的lambda中不会检查多余的属性

[英]TypeScript: excess properties are not checked in lambdas without explicit return type

In the following example im trying to figure out why my typing works for all parts of my object apart from my reducer return type? 在下面的示例中,我试图弄清楚为什么我的键入除了我的reducer返回类型之外还可以用于对象的所有部分?

If i explicitly set: reducer: (state, action): CounterState the compiler complains (as expected) that i'm not returning the right state. 如果我明确设置了: reducer: (state, action): CounterState则编译器会抱怨(如预期的那样)我未返回正确的状态。 The thing is, i don't see why i should have to do this seeing as i'm already enforcing this within my Config type?? 问题是,我不明白为什么我必须在我的Config类型中强制执行此操作?

The simplified example: 简化示例:

interface CounterState {
    counter: number;
}

type Reducer = () => CounterState

const reducer1: Reducer = () => ({
    counter: 1,
    foo: 'bar' // no errors, why?
})

const reducer2: Reducer = (): CounterState => ({
    counter: 1,
    foo: 'bar' // error: Object literal may only specify known properties
})

Type compatibility in TypeScript is based on structural subtyping. TypeScript中的类型兼容性基于结构子类型。

https://www.typescriptlang.org/docs/handbook/type-compatibility.html https://www.typescriptlang.org/docs/handbook/type-compatibility.html

Typescript designed to allow extra properties. 打字稿旨在允许额外的属性。 But in some places we have a little inconsistent behaviour and it claims object literal may only specify known properties . 但是在某些地方,我们的行为有些不一致,它声称object literal may only specify known properties Such behaviour is more expected but it is not a structural subtyping... 此类行为更可预期,但不是结构性子类型...

https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-interfaces-and-structural-typing-7fcecd54aef5 https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-interfaces-and-structural-typing-7fcecd54aef5

Finally I found the issue in GitHub, exactly about the problem. 最终,我在GitHub上找到了与该问题有关的问题。 In short: 简而言之:

Ideally this would be an error. 理想情况下,这将是一个错误。 Unfortunately it turns out to be very difficult to fix this without possibly having consequences in terms of runaway recursion and/or performance 不幸的是,事实证明,要解决此问题非常困难,而又可能不会导致失控的递归和/或性能方面的后果

Original answer: Since typescript 1.6, object literals mustn't have extra properties . 原始答案:自打字稿1.6起,对象文字不得具有额外的属性 But if you cast an object to the type, extra properties are allowed. 但是,如果将对象强制转换为类型,则允许使用其他属性。 For example: 例如:

const state: CounterState = {
    counter: 1,
    foo: "bar" // Error, unknown property 'foo'
};

const state2 = {
    counter: 1,
    foo: "bar" // no errors
} as CounterState

It looks very similar to your problem, when you specify the lambda return type explicitly, the first rule is applied. 它看起来非常类似于您的问题,当您明确指定lambda返回类型时,将应用第一个规则。 But, if the return type isn't specified, the compiler thinks: "ok, may be I can cast the object to the CounterState... Is it ok? I'm not sure... But, I will try!", and the second rule is applied. 但是,如果未指定返回类型,则编译器会认为:“好吧,可以将对象强制转换为CounterState吗?可以吗?我不确定...但是,我会尝试的!” ,并应用第二条规则。

But I cannot refer to any documentation or compiler specification, which describes such behaviour, I didn't found it too. 但是我无法参考描述此类行为的任何文档或编译器规范,我也没有找到它。

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

相关问题 显式函数返回类型创建功能组件在打字稿中做出反应 - explicit-function-return-type creating functional component react in typescript TypeScript 或者返回类型未检测到属性 - TypeScript either or return type is not detecting properties 使用 TypeScript 动态返回对象属性类型 - Dynamic return type of object properties using TypeScript TypeScript如何处理接口和类中的多余属性的区别 - Difference in how TypeScript handles excess properties in interfaces and classes 当将Suspense与React Router一起使用时,Typescript引发显式的函数返回类型错误 - Typescript throws an explicit function return type error when using Suspense with React Router 没有显式类型属性的动作 - actions without an explicit type property 缺少类型的属性(打字稿) - Missing properties for type (typescript) Typescript 具有和不具有显式类型的变量的验证规则 - Typescript validation rules for variables with and without explicit types 在Typescript中,为什么对象不能在赋值时指定多余的属性,但是当它使用相同的接口传递给函数时呢? - In Typescript, why is it that an object cannot specify excess properties on assignment but it can when passed to a function, using same Interface? 打字稿中的联合返回类型 - union return type in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM