#How come we can spread a tuple but we cannot do the same thing with object types?

12 messages · Page 1 of 1 (latest)

empty cipherBOT
#
raswonders#0

Preview:```ts
type T1 = [number, string];
type T2 = [boolean, ...T1];
type T3 = [...T1, ...T2];

type O1 = { name: "Ras" };
type O2 = { age: 69 };
type O3 = { ...O1, ...O2 };```

marsh dirge
#

I tried look up some official / unofficial docs but I didn't find anything which would give me insight on this one

summer basin
# empty cipher

tuples are ordered, you kinda have to specify the order. for objects you can just do O1 & O2

#

T1 is basically { 0: number, 1: string }, but T2 should be { 0: boolean, 1: number, 2: string }
that's a more complicated operation, compared to just merging the properties like for objects

marsh dirge
#
empty cipherBOT
#

@marsh dirge Here's a shortened URL of your playground link! You can remove the full link from your message.

raswonders#0

Preview:```ts
type T1 = [number, string];
type T2 = [boolean, ...T1];
type T3 = [...T1, ...T2];

type A1 = number[];
type A2 = number[];
type A3 = [...A1, ...A2]
// ^?

type O1 = { name: "Ras" };
type O2 = { age: 69 };
type O3 = { ...O1, ...O2 };
// ^?```

marsh dirge
#

for objects i get O1: any O2:any even though objects type declarations are right there...

summer basin
#

this syntax just isn't supported for object types.

#

because there's a more basic operation that does the same (well, slightly different) thing.

#

the indexing example is just explaining why array spread is a more complex operation than intersection

marsh dirge
summer basin
#

if you need the runtime behavior of duplicate keys in O2 overriding those in O1, you can use Omit<O1, keyof O2> & O2