- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试为 React 组件的 props 使用联合类型
type Props =
| {
type: "string"
value: string
}
| {
type: "number"
value: number
}
| {
type: "none"
}
class DynamicProps extends React.Component<Props> {
render() {
return null
}
}
// Ok
const string_jsx = <DynamicProps type="string" value="hello" />
// Error as expected, value should be a string
const string_jsx_bad = <DynamicProps type="string" value={5} />
// Ok
const number_jsx = <DynamicProps type="number" value={5} />
// Error as expcted value should be a number
const number_jsx_bad = <DynamicProps type="number" value="hello" />
// Error as expected, invalid isn't a property on any of the unioned types
const extra = <DynamicProps type="string" value="extra" invalid="what" />
// No error? There should be no value when type="none"
const none_jsx = <DynamicProps type="none" value="This should be an error?" />
// Ok, seems like value has become optional
const none2_jsx = <DynamicProps type="none" />
// Error as expected, value is not present. Value doesn't seem to be made optional all the time
const required = <DynamicProps type="string" />
它似乎部分起作用,因为根据 type
Prop 有效的 Prop 会改变。然而,虽然没有出现在联合中任何类型上的额外属性将是一个错误,但似乎出现在至少一个联合类型中但不属于基于判别属性的类型将不是一个错误错误。
我不确定为什么会这样。使用联合类型作为 React 组件的 props 是一种反模式吗?
最佳答案
问题与涉及工会时过多的属性(property)检查有关。你可以阅读这个答案 here到一个类似的问题。它的要点是联合的额外属性检查允许任何成员的任何键出现在对象上。我们可以通过引入 never 类型的额外成员来解决这个问题,以确保具有过多属性的对象不会错误地与特定成员兼容:
type Props =
| {
type: "string"
value: string
}
| {
type: "number"
value: number
}
| {
type: "none"
}
type UnionKeys<T> = T extends any ? keyof T : never;
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
type StrictUnion<T> = StrictUnionHelper<T, T>
class DynamicProps extends React.Component<StrictUnion<Props>> {
render() {
return null
}
}
// error now
const none_jsx = <DynamicProps type="none" value="This should be an error?" />
关于reactjs - typescript react : Union type for props does not display error when providing excess properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52771362/
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我正在用 TypeScript 构建一个 React 应用程序。我有一个 RequiresPermission基于谓词的组件应该渲染一个或另一个组件并转发所有 Prop 。 type Props =
我想通过 gatsby 布局传递我所有的 props。例如: import React, { Component } from 'react'; export default class Exampl
如果我使用合成属性,那我为什么不直接说: self.property = nil; 这将释放引用计数,并确保我没有悬挂指针。 看起来很简单,但我看到的 99% 的代码似乎都是这样做的: [proper
Eslint 抛出 eslint(react/prop-types) 错误,尽管已经声明了 propTypes。我正在使用 eslint-plugin-react 我研究了其他几个类似的问题以及 li
我正在使用以下由 linter eslint-plugin-react 解析的代码。它返回警告: "product is missing in props validation" 当我在底部的 pro
我正在尝试在 React 应用程序中添加 TypeScript。 版本: "react": "16.9.0", "typescript": "3.5.3", 我有一个像这样的数组 import aLo
我有一个组件 . 如果组件没有 this.props.children , 我想设置 Prop ariaLabel作为isRequired ,否则 in 可以是可选的。我该怎么做? ariaLabe
我应该用一个代替另一个吗?一起使用它们更好吗?谢谢。 最佳答案 prop in obj 检查 obj 是否有名为 prop 的属性,即使它只是从原型(prototype)继承而来。 obj.hasOw
我的组件 Text有 2 个 Prop :isHideable: boolean和 hidden: boolean .我如何允许 Hidden仅在 isHideable 时作为 Prop 是true
我试图将带有一些 Prop 的功能组件发送到另一个组件,并在接收组件中尝试键入检查该组件的某些 Prop 。这是代码: // BaseButton.tsx export type ButtonProp
是否可以从也作为 prop 传递的未知组件推断出正确的 props 类型? 如果已知组件(存在于当前文件中),我可以获得 Prop : type ButtonProps = React.Compone
我对 react 还很陌生,这是我正在努力解决的问题。 有一个父组件 家长 它将 Prop 传递给 child 。 其中一个 Prop ,包括一个要渲染的元素,如下所示: 在子组件中,我想获取这个组
我想做一个 Tabs推断 active 的可能值的组件prop 基于它的 child 拥有的东西 name Prop 。这就是我尝试这样做的方式: import React from 'react'
我对 react 还很陌生,并且只有当用户开始向下滚动时,我才尝试将更多信息加载到记录数组中。问题是新信息出现在数组中,但如果您尝试调用它,它将返回undefined。我在这里不明白什么? 父组件:
因此,如果我对一个组件有很多不同的 Prop ,我希望我可以做类似的事情 const { ...props } = props; 而不是 const { prop1, prop2, prop3, ..
这是我寻求指导的问题类型,因为我不确定我正在寻找的内容是否存在...... 上下文:我正在使用 Firestore 来保存数据,并且正在构建一个可重用的自定义 Hook (React 16.8),以便
我有一个 React 组件,它获取一个配置对象作为 prop,它看起来像这样: { active: true, foo: { bar: 'baz' } } 在
如何附加另一个属性?我有一个 react 组件,其中有人传入 ...props,我想附加一个额外的 Prop 最佳答案 请记住,传递 Prop 的顺序将决定将哪个值传递给该组件。这适用于有两个同名
我是一名优秀的程序员,十分优秀!