import type {KeyAliases, SimplifyObject} from './internal/index.d.ts'; import type {IsAny} from './is-any.d.ts'; import type {OmitIndexSignature} from './omit-index-signature.d.ts'; import type {PickIndexSignature} from './pick-index-signature.d.ts'; import type {RequiredKeysOf} from './required-keys-of.d.ts'; // Numeric properties and enum keys collide with their string forms, so compare normalized names and read the actual declared key. // The spread syntax always produces a fresh object literal, so `readonly` is dropped, and an index signature survives only when both operands have it. type SpreadObject< FirstType extends object, SecondType extends object, // The selected keys are type parameters constrained to `keyof`, so the mapped types below keep the optional modifiers of their properties. FirstKeys extends keyof FirstType = Exclude, KeyAliases>>>, SecondKeys extends keyof SecondType = RequiredKeysOf> | Exclude, KeyAliases>>, > = { // Required second properties replace the first property entirely, including its declared key spelling. Read the retained keys directly without building an intermediate `Omit` object. -readonly [Key in FirstKeys]: Key extends KeyAliases> ? FirstType[Key] | Required>[Extract, KeyAliases>] : FirstType[Key]; } & { -readonly [Key in SecondKeys]: SecondType[Key]; } & { [Key in keyof PickIndexSignature & keyof PickIndexSignature]: PickIndexSignature[Key] | PickIndexSignature[Key]; }; type TupleOrArray = readonly [...unknown[]]; type SpreadTupleOrArray< FirstType extends TupleOrArray, SecondType extends TupleOrArray, > = Array; type Spreadable = object | TupleOrArray; /** Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax. Note: For a numeric key and its string alias, the value type matches spread inference, but its spelling in `keyof` can differ. Use the key spelling declared by the result. @example ``` import type {Spread} from 'type-fest'; type Foo = { a: number; b?: string; }; type Bar = { b?: number; c: boolean; }; const foo = {a: 1, b: '2'}; const bar = {c: false}; const fooBar = {...foo, ...bar}; type FooBar = Spread; //=> {a: number; b?: string | number; c: boolean} declare function baz(argument: FooBar): void; baz(fooBar); ``` @example ``` import type {Spread} from 'type-fest'; const foo = [1, 2, 3]; const bar = ['4', '5', '6']; const fooBar = [...foo, ...bar]; type FooBar = Spread; // FooBar = (string | number)[] declare function baz(argument: FooBar): void; baz(fooBar); ``` @category Object */ export type Spread< FirstType extends Spreadable, SecondType extends Spreadable, > = // The spread syntax infers `any` when either operand is `any`, so this has to as well. Without the guard the mapped type walked `keyof any`, which is `string | number | symbol`, and the result kept the other operand's own keys only, so it accepted strictly less than the runtime does. `ObjectMerge`, the sound version of the same merge, answers `any` for the same input. IsAny extends true ? any : IsAny extends true ? any : FirstType extends TupleOrArray ? SecondType extends TupleOrArray ? SpreadTupleOrArray : SecondType extends unknown // For distributing `SecondType` ? SimplifyObject> : never : SecondType extends unknown // For distributing `SecondType` ? SimplifyObject> : never; export {};