-
-
Notifications
You must be signed in to change notification settings - Fork 554
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
5374588
commit 4045737
Showing
20 changed files
with
368 additions
and
27 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,24 @@ | ||
import type {IsAny} from './is-any'; | ||
|
||
/** | ||
An if-else-like type that resolves depending on whether the given type is `any`. | ||
@see {@link IsAny} | ||
@example | ||
``` | ||
import type {IfAny} from 'type-fest'; | ||
type ShouldBeTrue = IfAny<any>; | ||
//=> true | ||
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>; | ||
//=> 'bar' | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = ( | ||
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny | ||
); |
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,24 @@ | ||
import type {IsNever} from './is-never'; | ||
|
||
/** | ||
An if-else-like type that resolves depending on whether the given type is `never`. | ||
@see {@link IsNever} | ||
@example | ||
``` | ||
import type {IfNever} from 'type-fest'; | ||
type ShouldBeTrue = IfNever<never>; | ||
//=> true | ||
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>; | ||
//=> 'bar' | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = ( | ||
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever | ||
); |
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,24 @@ | ||
import type {IsUnknown} from './is-unknown'; | ||
|
||
/** | ||
An if-else-like type that resolves depending on whether the given type is `unknown`. | ||
@see {@link IsUnknown} | ||
@example | ||
``` | ||
import type {IfUnknown} from 'type-fest'; | ||
type ShouldBeTrue = IfUnknown<unknown>; | ||
//=> true | ||
type ShouldBeBar = IfUnknown<'not unknown', 'foo', 'bar'>; | ||
//=> 'bar' | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IfUnknown<T, TypeIfUnknown = true, TypeIfNotUnknown = false> = ( | ||
IsUnknown<T> extends true ? TypeIfUnknown : TypeIfNotUnknown | ||
); |
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 @@ | ||
/** | ||
Returns a boolean for whether the given type is `any`. | ||
@link https://stackoverflow.com/a/49928360/1490091 | ||
Useful in type utilities, such as disallowing `any`s to be passed to a function. | ||
@example | ||
``` | ||
import type {IsAny} from 'type-fest'; | ||
const typedObject = {a: 1, b: 2} as const; | ||
const anyObject: any = {a: 1, b: 2}; | ||
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) { | ||
return obj[key]; | ||
} | ||
const typedA = get(typedObject, 'a'); | ||
//=> 1 | ||
const anyA = get(anyObject, 'a'); | ||
//=> any | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IsAny<T> = 0 extends 1 & T ? true : false; |
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,49 @@ | ||
/** | ||
Returns a boolean for whether the given type is `never`. | ||
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919 | ||
@link https://stackoverflow.com/a/53984913/10292952 | ||
@link https://www.zhenghao.io/posts/ts-never | ||
Useful in type utilities, such as checking if something does not occur. | ||
@example | ||
``` | ||
import type {IsNever} from 'type-fest'; | ||
type And<A, B> = | ||
A extends true | ||
? B extends true | ||
? true | ||
: false | ||
: false; | ||
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts | ||
type AreStringsEqual<A extends string, B extends string> = | ||
And< | ||
IsNever<Exclude<A, B>> extends true ? true : false, | ||
IsNever<Exclude<B, A>> extends true ? true : false | ||
>; | ||
type EndIfEqual<I extends string, O extends string> = | ||
AreStringsEqual<I, O> extends true | ||
? never | ||
: void; | ||
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> { | ||
if (input === output) { | ||
process.exit(0); | ||
} | ||
} | ||
endIfEqual('abc', 'abc'); | ||
//=> never | ||
endIfEqual('abc', '123'); | ||
//=> void | ||
``` | ||
@category Type Guard | ||
@category Utilities | ||
*/ | ||
export type IsNever<T> = [T] extends [never] ? true : false; |
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,52 @@ | ||
import type {IsNull} from './internal'; | ||
|
||
/** | ||
Returns a boolean for whether the given type is `unknown`. | ||
@link https://github.com/dsherret/conditional-type-checks/pull/16 | ||
Useful in type utilities, such as when dealing with unknown data from API calls. | ||
@example | ||
``` | ||
import type {IsUnknown} from 'type-fest'; | ||
// https://github.com/pajecawav/tiny-global-store/blob/master/src/index.ts | ||
type Action<TState, TPayload = void> = | ||
IsUnknown<TPayload> extends true | ||
? (state: TState) => TState, | ||
: (state: TState, payload: TPayload) => TState; | ||
class Store<TState> { | ||
constructor(private state: TState) {} | ||
execute<TPayload = void>(action: Action<TState, TPayload>, payload?: TPayload): TState { | ||
this.state = action(this.state, payload); | ||
return this.state; | ||
} | ||
// ... other methods | ||
} | ||
const store = new Store({value: 1}); | ||
declare const someExternalData: unknown; | ||
store.execute(state => ({value: state.value + 1})); | ||
//=> `TPayload` is `void` | ||
store.execute((state, payload) => ({value: state.value + payload}), 5); | ||
//=> `TPayload` is `5` | ||
store.execute((state, payload) => ({value: state.value + payload}), someExternalData); | ||
//=> Errors: `action` is `(state: TState) => TState` | ||
``` | ||
@category Utilities | ||
*/ | ||
export type IsUnknown<T> = ( | ||
unknown extends T // `T` can be `unknown` or `any` | ||
? IsNull<T> extends false // `any` can be `null`, but `unknown` can't be | ||
? true | ||
: false | ||
: false | ||
); |
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
Oops, something went wrong.