-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
017bf38
commit 964466c
Showing
4 changed files
with
53 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,37 @@ | ||
import type {Except} from './except'; | ||
import type {Simplify} from './simplify'; | ||
|
||
/** | ||
Create a type that changes the type of the given keys. | ||
Use-cases: | ||
- Creating variations of a base model. | ||
- Fixing incorrect external types. | ||
@see `Merge` if you need to change multiple properties to different types. | ||
@example | ||
``` | ||
import type {SetFieldType} from 'type-fest'; | ||
type MyModel = { | ||
id: number; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
}; | ||
type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>; | ||
// { | ||
// id: number; | ||
// createdAt: string; | ||
// updatedAt: string; | ||
// } | ||
``` | ||
@category Object | ||
*/ | ||
export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType> = | ||
Simplify< | ||
Except<BaseType, Keys> & | ||
Record<Keys, NewType> | ||
>; |
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,14 @@ | ||
import {expectNotAssignable, expectType} from 'tsd'; | ||
import type {SetFieldType} from '../index'; | ||
|
||
declare const variation1: SetFieldType<{a: number}, 'a', string>; | ||
expectType<{a: string}>(variation1); | ||
|
||
declare const variation2: SetFieldType<{a: number; b: boolean; c: Date}, 'a' | 'b', string>; | ||
expectType<{a: string; b: string; c: Date}>(variation2); | ||
|
||
declare const variation3: SetFieldType<{a: string; b: boolean; c: Date}, 'b' | 'c', number>; | ||
expectType<{a: string; b: number; c: number}>(variation3); | ||
|
||
declare const variation4: SetFieldType<{a: string; b: string; c: string}, 'b', number>; | ||
expectNotAssignable<{a: string; b: string; c: string}>(variation4); |