Skip to content

Commit 964466c

Browse files
lucastelessindresorhus
andauthoredOct 25, 2023
Add SetFieldType type (#721)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 017bf38 commit 964466c

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
 

‎index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export type {IsUnknown} from './source/is-unknown';
101101
export type {IfUnknown} from './source/if-unknown';
102102
export type {ArrayIndices} from './source/array-indices';
103103
export type {ArrayValues} from './source/array-values';
104+
export type {SetFieldType} from './source/set-field-type';
104105

105106
// Template literal types
106107
export type {CamelCase} from './source/camel-case';

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ Click the type names for complete docs.
175175
- [`IntRange`](source/int-range.d.ts) - Generate a union of numbers.
176176
- [`ArrayIndices`](source/array-indices.d.ts) - Provides valid indices for a constant array or tuple.
177177
- [`ArrayValues`](source/array-values.d.ts) - Provides all values for a constant array or tuple.
178+
- [`SetFieldType`](source/set-field-type.d.ts) - Create a type that changes the type of the given keys.
178179

179180
### Type Guard
180181

‎source/set-field-type.d.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {Except} from './except';
2+
import type {Simplify} from './simplify';
3+
4+
/**
5+
Create a type that changes the type of the given keys.
6+
7+
Use-cases:
8+
- Creating variations of a base model.
9+
- Fixing incorrect external types.
10+
11+
@see `Merge` if you need to change multiple properties to different types.
12+
13+
@example
14+
```
15+
import type {SetFieldType} from 'type-fest';
16+
17+
type MyModel = {
18+
id: number;
19+
createdAt: Date;
20+
updatedAt: Date;
21+
};
22+
23+
type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>;
24+
// {
25+
// id: number;
26+
// createdAt: string;
27+
// updatedAt: string;
28+
// }
29+
```
30+
31+
@category Object
32+
*/
33+
export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType> =
34+
Simplify<
35+
Except<BaseType, Keys> &
36+
Record<Keys, NewType>
37+
>;

‎test-d/set-field-type.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {expectNotAssignable, expectType} from 'tsd';
2+
import type {SetFieldType} from '../index';
3+
4+
declare const variation1: SetFieldType<{a: number}, 'a', string>;
5+
expectType<{a: string}>(variation1);
6+
7+
declare const variation2: SetFieldType<{a: number; b: boolean; c: Date}, 'a' | 'b', string>;
8+
expectType<{a: string; b: string; c: Date}>(variation2);
9+
10+
declare const variation3: SetFieldType<{a: string; b: boolean; c: Date}, 'b' | 'c', number>;
11+
expectType<{a: string; b: number; c: number}>(variation3);
12+
13+
declare const variation4: SetFieldType<{a: string; b: string; c: string}, 'b', number>;
14+
expectNotAssignable<{a: string; b: string; c: string}>(variation4);

0 commit comments

Comments
 (0)
Please sign in to comment.