Given some object TInput
, and some value TOutputValue
, allows any key
of TInput
that can be assigned to TOutputValue
.
▶Example 1
Example 1
import { AllowInputKeyIfInputCanExtendOutput } from "./types.ts"; interface SomeInput { inString: string; inStringOrUndefined: string | undefined; inNumber: number; } type SomeOutput = string | undefined; type AllowedKey = AllowInputKeyIfInputCanExtendOutput<SomeInput, SomeOutput>; let key: AllowedKey = "inString"; key = "inStringOrUndefined"; // key = "inNumber"; // type error
[TInputKey in keyof TInput]: TInput[TInputKey] extends TOutputValue ? TInputKey : never[keyof TInput]