-
-
Notifications
You must be signed in to change notification settings - Fork 550
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
6 changed files
with
121 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,29 @@ | ||
import type {IsEqual} from './is-equal'; | ||
|
||
/** | ||
Extract all readonly keys from the given type. | ||
This is useful when you want to create a new type that contains readonly keys only. | ||
@example | ||
``` | ||
import type {ReadonlyKeysOf} from 'type-fest'; | ||
interface User { | ||
name: string; | ||
surname: string; | ||
readonly id: number; | ||
} | ||
type UpdateResponse<Entity extends object> = Pick<Entity, ReadonlyKeysOf<Entity>>; | ||
const update1: UpdateResponse<User> = { | ||
id: 123, | ||
}; | ||
``` | ||
@category Utilities | ||
*/ | ||
export type ReadonlyKeysOf<T> = NonNullable<{ | ||
[P in keyof T]: IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends true ? P : never | ||
}[keyof T]>; |
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,30 @@ | ||
import type {IsEqual} from './is-equal'; | ||
|
||
/** | ||
Extract all writable keys from the given type. | ||
This is useful when you want to create a new type that contains writable keys only. | ||
@example | ||
``` | ||
import type {WritableKeysOf} from 'type-fest'; | ||
interface User { | ||
name: string; | ||
surname: string; | ||
readonly id: number; | ||
} | ||
type UpdateRequest<Entity extends object> = Pick<Entity, WritableKeysOf<Entity>>; | ||
const update1: UpdateRequest<User> = { | ||
name: 'Alice', | ||
surname: 'Acme', | ||
}; | ||
``` | ||
@category Utilities | ||
*/ | ||
export type WritableKeysOf<T> = NonNullable<{ | ||
[P in keyof T]: IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends false ? P : never | ||
}[keyof T]>; |
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,29 @@ | ||
import {expectType} from 'tsd'; | ||
import type {ReadonlyKeysOf} from '../index'; | ||
|
||
type TestType1 = { | ||
a: string; | ||
readonly b: boolean; | ||
}; | ||
|
||
type TestType2 = { | ||
readonly a: string; | ||
readonly b: boolean; | ||
}; | ||
|
||
type TestType3 = { | ||
a: string; | ||
b: boolean; | ||
}; | ||
|
||
type ReadonlyKeysOf1 = ReadonlyKeysOf<TestType1>; | ||
type ReadonlyKeysOf2 = ReadonlyKeysOf<TestType2>; | ||
type ReadonlyKeysOf3 = ReadonlyKeysOf<TestType3>; | ||
|
||
declare const test1: ReadonlyKeysOf1; | ||
declare const test2: ReadonlyKeysOf2; | ||
declare const test3: ReadonlyKeysOf3; | ||
|
||
expectType<'b'>(test1); | ||
expectType<'a' | 'b'>(test2); | ||
expectType<never>(test3); |
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,29 @@ | ||
import {expectType} from 'tsd'; | ||
import type {WritableKeysOf} from '../index'; | ||
|
||
type TestType1 = { | ||
readonly a: string; | ||
b: boolean; | ||
}; | ||
|
||
type TestType2 = { | ||
a: string; | ||
b: boolean; | ||
}; | ||
|
||
type TestType3 = { | ||
readonly a: string; | ||
readonly b: boolean; | ||
}; | ||
|
||
type WritableKeysOf1 = WritableKeysOf<TestType1>; | ||
type WritableKeysOf2 = WritableKeysOf<TestType2>; | ||
type WritableKeysOf3 = WritableKeysOf<TestType3>; | ||
|
||
declare const test1: WritableKeysOf1; | ||
declare const test2: WritableKeysOf2; | ||
declare const test3: WritableKeysOf3; | ||
|
||
expectType<'b'>(test1); | ||
expectType<'a' | 'b'>(test2); | ||
expectType<never>(test3); |