Typescript mapped type with filtered keys

If I have a type like: type T = { a: string, _a: string, b: number, _c: boolean, /* … any number of unknown properties … */ }; Is there anyway I can define a new mapped type which has all (and only) properties of T which do not begin with the letter _? For… Read More Typescript mapped type with filtered keys

Extend an interface with new set of properties based on original interface with mapped types

Supposed I have the following interface: interface Person { name: string; age: number; location: string; } I want to extend it to have 3 additional properties, one for each of the interfaces’ original properties. So final outcome I want is: interface Person { name: string; nameHidden: boolean; age: number; ageHidden: boolean; location: string; locationHidden: boolean;… Read More Extend an interface with new set of properties based on original interface with mapped types

How to create a Typescript type based on the keys/values of a dictionary?

Is there a way to automatically generate a type based on a dictionary value? E.g. given this dictionary of constant values: export const SHIPMETHODS: OptionsMap<UpsShipMethodOption> = { ‘UPS’: ‘GND’, ‘UPS AIR2’: ‘2DA’, ‘UPS 3DAY SELECT’: ‘3DS’ } Is there a way to automatically generate a type like this, without having to duplicate everything? export type… Read More How to create a Typescript type based on the keys/values of a dictionary?

Record Type Reverser

I’d like to reverse the property names and types for a record that has unique constant property values. const VALUE = { field1: "fieldA", field2: "fieldB" } as const type Reversed = Reverser<typeof VALUE> //which should yield the following type type Reversed = { fieldA: "field1"; fieldB: "field2"; } I dont think it is possible… Read More Record Type Reverser