Sha256: 42ca885a3c8ffdffcd9df252582aef9433438cf545a148e3a5e7568ca8575a56

Contents?: true

Size: 1.36 KB

Versions: 28

Compression:

Stored size: 1.36 KB

Contents

import type {IsNull} from './is-null';

/**
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
);

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/type-fest/source/is-unknown.d.ts