简体   繁体   English

React Component 精确 TypeScript 道具类型

[英]React Component Exact TypeScript Prop Types

When I have this:当我有这个:

interface PropTypes {
   user?: {
      name?: string,
      age?: number
   }
}
const Comp = (props: PropTypes) => ...

Why this works:为什么这有效:

const user = {
   random: 3,
   name: 'test'
}
<Comp user={user} />  // works

But this does not (moved value from variable to here):但这不是(将值从变量移到此处):

<Comp user={{ random: 3,  name: 'test' }} />   // does not work

Also when I delete all known properties from user it does not work anymore:此外,当我从用户中删除所有已知属性时,它不再起作用:

const user = {
   random: 3
}
<Comp user={user} />   // does not work

I want to know why it works like this.我想知道为什么它会这样工作。 Why it accepts the "random" prop, that is unknown for him when it is in variable.为什么它接受“随机”道具,当它处于变量状态时,他不知道。 Why variable type has different behaviour than directly passing prop?为什么变量类型与直接传递 prop 有不同的行为? Is there any way to allow only exact props without giving type each time to the variable?有没有办法只允许精确的道具而不每次都给变量类型?

This has to do with Typescript's Excess Property Checking .这与 Typescript 的Excess Property Checking有关。

Structural Type System: TypeScript is fine with having extra properties as long as you provide a type with its' required properties.结构类型系统:TypeScript 可以提供额外的属性,只要您提供具有所需属性的类型即可。

When TypeScript encounters object literal in assignments or when passed as an argument to functions it triggers an action called Excess Property Checking .当 TypeScript 在赋值中遇到 object 文字或作为参数传递给函数时,它会触发一个名为Excess Property Checking的操作。 Contrary to structural typing, it check whether the object has the exact properties.与结构类型相反,它检查 object 是否具有确切的属性。

One interesting behavior is with “ Weak Types ” — types which all their fields are marked as Optional.一个有趣的行为是“弱类型”——所有字段都标记为可选的类型。 In this case the excess property checking an action takes place even when assigning intermediate variable.在这种情况下,即使在分配中间变量时也会发生额外的属性检查操作。

You can read more at this link .您可以在此链接阅读更多内容。

Case 1 - Structural Type System案例 1 - 结构类型系统

const user = {
   random: 3,
   name: 'test'
}
<Comp user={user} />  // works

In this case the type of user is inferred as:在这种情况下, user类型被推断为:

{
  random: number;
  name: string;
}

This type is compatible with PropTypes , so no error.此类型与PropTypes兼容,因此没有错误。

Case 2 - Excess Property Checking案例 2 - 超额财产检查

Passing object literal in assignments:在作业中传递 object 文字:

<Comp user={{ random: 3,  name: 'test' }} />   // does not work

In this case random is not a valid property for type PropTypes , so typescript tells you adding random is an error.在这种情况下, random不是PropTypes类型的有效属性,因此 typescript 告诉您添加random是错误的。

Object literal may only specify known properties, and 'random' does not exist in type '{ name?: string | Object literal 可能只指定已知属性,'random' 不存在于类型 '{ name?: string | undefined;不明确的; age?: number |年龄?:数字 | undefined;不明确的; }' }'

Case 3 - Weak Types案例 3 - 弱类型

const user = {
   random: 3
}
<Comp user={user} />   // does not work

In this case typescript inferred the type of user as:在这种情况下,typescript 将用户类型推断为:

{
  random: number;
}

Also in this case random is an invalid property for PropTypes , so this is an error.同样在这种情况下, randomPropTypes的无效属性,因此这是一个错误。

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

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