Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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,有两个不同对象的数组(数组a和数组b),这些数组的并集的成员值–(a | b)[number]–生成它们的交集,而我期望的并集是: type a = { one: string }[]; type b = { one: string, two: string }[]; type ab = (a | b)[number]; // actual type: { one: string } // expected type: { one: s

有两个不同对象的数组(数组
a
和数组
b
),这些数组的并集的成员值–
(a | b)[number]
–生成它们的交集,而我期望的并集是:

type a = { one: string }[];
type b = { one: string, two: string }[];

type ab = (a | b)[number]; // actual type:   { one: string }
                           // expected type: { one: string } | { one: string, two: string }
另见a

我想知道:

  • 派生类型
    {one:string}
    正确吗
  • 为什么它不是
    {one:string}{one:string,two:string}
  • 是否有一种方法可以实现任意对象数组的
    预期
    类型

  • 谢谢

    是的,派生类型是正确的,但通常不是预期的

    这就是工会在TS中的工作方式

    考虑下一个例子:

    type a={one:string};
    类型b={one:string,two:string};
    ab型=keyof(a | b);//“一个”
    
    TS将返回
    one
    ,因为此键可在两种类型(a、b)之间共享

    为了达到预期的行为,您可以尝试:

    当条件类型作用于泛型类型时,当给定一个联合类型时,它们将成为分布式的

    type a={one:string}[];
    类型b={one:string,two:string}[];
    类型ArrayValues=T扩展数组?永远不会
    
    type Result=ArrayValues

    是,派生类型是正确的,但通常不是预期的

    这就是工会在TS中的工作方式

    考虑下一个例子:

    type a={one:string};
    类型b={one:string,two:string};
    ab型=keyof(a | b);//“一个”
    
    TS将返回
    one
    ,因为此键可在两种类型(a、b)之间共享

    为了达到预期的行为,您可以尝试:

    当条件类型作用于泛型类型时,当给定一个联合类型时,它们将成为分布式的

    type a={one:string}[];
    类型b={one:string,two:string}[];
    类型ArrayValues=T扩展数组?永远不会
    
    键入Result=ArrayValues

    太棒了,谢谢!在这种情况下,TS的行为感觉非常不直观。在这里,您可以找到一些有趣的typesAwesome,谢谢!在这种情况下,TS行为感觉非常不直观。在这里您可以找到一些有趣的类型