Middle+
type Foo = { a: string; b: number; c: null; d: boolean; } // FooKeys type FooKeys = keyof Foo // How to declare a new type based on FooKeys, but without the values b and d? type FooKeysWithoutBD
sobes.tech AI
Answer from AI
To declare a type FooKeysWithoutBD that contains the keys of type Foo but without the values b and d, you can use the TypeScript utility Exclude.
// Original type
interface Foo {
a: string;
b: number;
c: null;
d: boolean;
}
// All keys of Foo
type FooKeys = keyof Foo; // 'a' | 'b' | 'c' | 'd'
// Exclude 'b' and 'd'
type FooKeysWithoutBD = Exclude<FooKeys, 'b' | 'd'>; // 'a' | 'c'
Exclude<T, U> creates a new type by excluding from T all types that are assignable to U. In this case, the keys 'b' and 'd' are excluded from Foo.