typescript - TypeScript 覆盖现有值并强制执行字段值
问题描述
我一直在试图弄清楚如何扩展接口的可选值,这样如果一个字段设置为特定值,那么另一个字段必须存在,我无法弄清楚——这是怎么做到的?
目标是这样的:
- If
restricted
istrue
thenapiData
仍然是可选的。 - 如果
apiData
已设置,则restricted
必须设置为true
。
interface IRoute {
path?: string
restricted?: boolean
exact?: boolean
component: string
layout?: string
apiData?: string
}
interface IRouteWithData extends Omit<IRouteConfigItem, 'restricted apiData'> {
restricted: true
apiData: FC
}
type TRoute = IRouteWithData | IRouteConfigItem
const routeConfig: TRoute[] = [
{
path: '/foo',
restricted: false, // this should be an error
exact: true,
component: 'something',
apiData: 'some data'
},
{
path: '/bar',
restricted: true, // this is fine
exact: true,
component: 'something'
}
]
解决方案
所以解决方案是求和类型,您使用并集很接近,但我们需要定义判别式以允许 TS 推断差异。
// all fields which are static in all sum elements
interface IRouteBase {
path?: string
exact?: boolean
component: string
layout?: string
}
// type representing that if restricted is true - apiData needs to be set
interface IRouteWithData extends IRouteBase {
restricted: true
apiData: string
}
// type representing that if restricted is false- apiData is optional
interface IRouteMaybeData extends IRouteBase {
restricted: false
apiData?: string
}
type TRoute = IRouteWithData | IRouteMaybeData;
// example use
const mustHaveApiData: TRoute = {
component: 'component',
restricted: true, // error you need to set apiData
}
const notNeedHaveApiData: TRoute = {
component: 'component',
restricted: false, // no error, apiData is optional
}
有关使用 sum 类型的更多信息包含在我的文章中 - https://dev.to/macsikora/more-accurate-the-type-better-the-code-202l。享受!
推荐阅读
- jestjs - 有没有办法以编程方式创建 Gitlab CI 测试?
- python-3.x - Django Unitest 注册基于类的视图
- reactjs - 如何正确输入我的反应上下文提供者?
- python - 在 kivy 中显示列表
- r - 如何对一个数据框中的列值求和并将结果添加为另一个数据框中的列?
- java - 如何使用 SQS 事件对 aws lambda 进行单元测试
- docker - 拥有非 root 用户时的文件所有者
- c# - VSCode 未显示 MonoDevelop/Unity/C# 的建议代码
- c# - 无法访问已处置的对象。对象名称:AspNet Core/EF Core 项目中的“IServiceProvider”错误
- python - 无法按分位数选择 Pandas DataFrame