TypeScript provide default values for missing keys of a Type

benhowdle89

There might be an incredibly obvious answer, but how does one do the following:

type testStatus = {
    foo: boolean;
    bar: boolean;
    baz: boolean;
}

const makeDefaults = (o: Partial<testStatus>) => {
   const keys = // array of `testStatus` keys?
   keys.forEach(k => !(k in o) && (o[k] = false)) // provide a default value for missing keys
   return o
}

const obj: Partial<testStatus> = { foo: true, bar: false } 

const defaultedObj = makeDefaults(obj) // { foo: true, bar: false, baz: false }

Basically retrieve all the keys from a Type, to loop through?

Aleksey L.

You can't get keys as value from type because type doesn't exist at runtime. If type is simple and has same value type for all its keys as in example - you can create an array of keys first, then create the type from it:

const KEYS = ['foo', 'bar', 'baz'] as const;

type testStatus = { [K in typeof KEYS[number]]: boolean };

const makeDefaults = (o: Partial<testStatus>) => {
    KEYS.forEach(k => !(k in o) && (o[k] = false))
    return o
}

Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jackson deserialize default values missing

Typescript interface default values

typescript type for object with unknown keys, but only numeric values?

Typescript: Map with keys being the same type as values without restricting type of keys

TypeScript: How to create an interface for an object with many keys of the same type and values of the same type?

Adding dictionary values with the missing values in the keys

Replace values in pandas column with default value for missing keys

pykalman: (default) handling of missing values

Idiomatic TypeScript to provide default in case of [undefined,undefined]?

Unable to find the default constructor on type ClassName. Please provide the missing constructor

Does createMuiTheme provide default values for missing properties?

"type is missing the following properties from another type" when assign values with expected data shape in typescript

Typescript: destructure entire props object and provide default values using ES6

TypeScript type with specific set of keys and values

TypeScript: Cannot understand this error: Type: Foo is missing the following properties from type Bar add, addAll, delete, keys, and 3 more

Pick Values from JSON Object Where Keys in Typescript Type

Typescript: Generate Type from Object keys and Array string values

How do I provide default values to optional nested props of type Object in typescript + React?

Typescript: Use object values as keys for a new type

Replace missing values with default one

Typescript type for two types with same keys and similar values

TypeScript: How can I extract from a type only the keys and values of type array

Typescript Record type with string keys infers invalid type for it's values

Typescript: How to declare a type of array whose values are equal to keys of object?

How to limit the keys of a TypeScript type?

Missing properties for type (typescript)

Typescript: Type alias results in strange type when returning object with same keys but different values

Pulling keys out of typescript type/interface where the values are of type T

Typescript filtering object values by keys