Skip to main content
Module

x/scaffold/src/deps/types.ts>ConditionalKeys

scaffold your next project with style and 💗
Latest
type alias ConditionalKeys
import { type ConditionalKeys } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

Extract the keys from a type where the value type of the key extends the given Condition.

Internally this is used for the ConditionalPick and ConditionalExcept types.

Examples

Example 1

import type {ConditionalKeys} from 'type-fest';

interface Example {
	a: string;
	b: string | number;
	c?: string;
	d: {};
}

type StringKeysOnly = ConditionalKeys<Example, string>;
//=> 'a'

To support partial types, make sure your Condition is a union of undefined (for example, string | undefined) as demonstrated below.

Example 2

import type {ConditionalKeys} from 'type-fest';

type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
//=> 'a' | 'c'

Type Parameters

Base
Condition
definition: NonNullable<[Key in keyof Base]: Base[Key] extends Condition ? Key : never[keyof Base]>