Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 根据另一个字段类型有条件地设置字段类型_Typescript - Fatal编程技术网

Typescript 根据另一个字段类型有条件地设置字段类型

Typescript 根据另一个字段类型有条件地设置字段类型,typescript,Typescript,当另一个字段类型为X时,是否可以将该字段设置为可选字段 我希望action.meta在payload为数组时为never,在为对象时为{resourceId:string} const ADD_RESOURCES = 'ADD_RESOURCES' type Resource = { id: string } type AddResource<R = any> = { type: typeof ADD_RESOURCES payload: R extends [] ? R

当另一个字段类型为X时,是否可以将该字段设置为可选字段

我希望
action.meta
payload
为数组时为
never
,在
对象时为
{resourceId:string}

const ADD_RESOURCES = 'ADD_RESOURCES'

type Resource = { id: string }

type AddResource<R = any> = {
  type: typeof ADD_RESOURCES
  payload: R extends [] ? Resource[] : (Resource | null)
  meta: R extends [] ? never : { // when payload is an array, make meta never
    resourceId: string
  }
}

const action: Resource = {
  type: ADD_RESOURCES,
  payload: [{ id: '1' }]
  // allow me to omit meta when the payload is array
}

if (Array.isArray(action.payload)) {
  action.meta // make action.meta never
} else {
  action.meta // make action.meta { resourceId: string }
}
const ADD_RESOURCES='ADD_RESOURCES'
类型资源={id:string}
类型AddResource={
类型:添加资源的类型
有效负载:R扩展[]?资源[]:(资源|空)
meta:R扩展[]?从不:{//当有效负载是数组时,使meta从不
资源ID:字符串
}
}
常量操作:资源={
类型:添加资源,
有效负载:[{id:'1'}]
//当有效负载为数组时,请允许我省略meta
}
if(Array.isArray(action.payload)){
action.meta//make action.meta从不
}否则{
action.meta//make action.meta{resourceId:string}
}

在这种情况下,最好使用一个接头。由于您根据
有效负载
属性是否为数组来区分,因此无法真正使用区分的联合模式来缩小
操作
,您将需要使用自定义类型的保护:

const ADD_RESOURCES = 'ADD_RESOURCES'

type Resource = { id: string }

type AddResource = {
    type: typeof ADD_RESOURCES
    payload: Resource | null,
    meta: { // when payload is an array, make meta never
        resourceId: string
    }
} | {
    type: typeof ADD_RESOURCES
    payload: Resource[],
    meta: never // you can just omit this.. or type it as undefined 
}

const action: AddResource = {
  type: ADD_RESOURCES,
  payload: [{ id: '1' }]
} as AddResource // Assertion here so flow analasys doesn't just use the actual type of the object literal instead of AddResource

function isArrayResource(r: AddResource): r is Extract<AddResource, { payload: Resource[] }> {
    return Array.isArray(action.payload);
}

if (isArrayResource(action)) {
  action.meta // action.meta never
} else {
  action.meta //  action.meta { resourceId: string }
}
const ADD_RESOURCES='ADD_RESOURCES'
类型资源={id:string}
类型AddResource={
类型:添加资源的类型
有效负载:资源|空,
meta:{//当有效负载是数组时,使meta永不
资源ID:字符串
}
} | {
类型:添加资源的类型
有效载荷:资源[],
meta:never//您可以省略它..或者将其键入为未定义
}
常量操作:AddResource={
类型:添加资源,
有效负载:[{id:'1'}]
}此处为AddResource//断言,因此flow Anasys不只是使用对象文本的实际类型,而不是AddResource
函数isArrayResource(r:AddResource):r是提取