Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type {Except} from './except'; | ||
import type {RequiredKeysOf} from './required-keys-of'; | ||
import type {Simplify} from './simplify'; | ||
|
||
type Spread_<FirstType extends object, SecondType extends object> = { | ||
[Key in keyof FirstType]: Key extends keyof SecondType | ||
? FirstType[Key] | Required<SecondType>[Key] | ||
: FirstType[Key]; | ||
} & Pick< | ||
SecondType, | ||
RequiredKeysOf<SecondType> | Exclude<keyof SecondType, keyof FirstType> | ||
>; | ||
|
||
/** | ||
Mimic the type inferred by TypeScript when merging two objects using the spread operator. | ||
@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<Foo, Bar>; | ||
// type FooBar = { | ||
// a: number; | ||
// b?: string | number | undefined; | ||
// c: boolean; | ||
// } | ||
const baz = (argument: FooBar) => { | ||
// Do something | ||
} | ||
baz(fooBar); | ||
``` | ||
@category Object | ||
*/ | ||
export type Spread< | ||
FirstType extends object, | ||
SecondType extends object, | ||
> = Simplify<Spread_<FirstType, SecondType>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {expectType} from 'tsd'; | ||
import type {Spread} from '../index'; | ||
|
||
type Foo = { | ||
a: 'a1'; | ||
b?: 'b1'; | ||
c: 'c1'; | ||
d?: 'd1'; | ||
e: 'e1' | undefined; | ||
f: 'f1'; | ||
g?: 'g1'; | ||
}; | ||
|
||
type Bar = { | ||
a?: 'a2'; | ||
b: 'b2'; | ||
c: 'c2'; | ||
d?: 'd2'; | ||
e?: 'e2'; | ||
h: 'h2'; | ||
i?: 'i2'; | ||
}; | ||
|
||
type FooBar = Spread<Foo, Bar>; | ||
|
||
const foo: Foo = { | ||
a: 'a1', | ||
b: 'b1', | ||
c: 'c1', | ||
d: 'd1', | ||
e: 'e1', | ||
f: 'f1', | ||
g: 'g1', | ||
}; | ||
|
||
const bar: Bar = { | ||
b: 'b2', | ||
c: 'c2', | ||
h: 'h2', | ||
}; | ||
|
||
const foobar = {...foo, ...bar}; | ||
|
||
expectType<FooBar>(foobar); |