Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript interface for objects with some known and some unknown property names

Tags:

typescript

I have an object where all the keys are string, some of the values are string and the rest are objects in this form:

var object = {     "fixedKey1": "something1",     "fixedKey2": "something2",     "unknownKey1": { 'param1': [1,2,3], 'param2': "some2", 'param3': 'some3'},     "unknownKey2": { 'param1': [1,2,3], 'param2': "some2", 'param3': 'some3'},     "unknownKey3": { 'param1': [1,2,3], 'param2': "some2", 'param3': 'some3'},     ...     ... }; 

In this object fixedKey1 and fixedKey2 are the known keys which will be there in that object. unknownKey - value pair can vary from 1-n.

I tried defining the interface of the object as:

interface IfcObject {     [keys: string]: {         param1: number[];         param2: string;          param3: string;   } } 

But this throws the following error:

Variable of type number is not assignable of type object

Which I found out that it is not able to assign this interface to "fixedKey - value" pair.

So, how can I do the type checking of this kind of variables?

like image 545
yugantar kumar Avatar asked Jul 08 '16 06:07

yugantar kumar


People also ask

Can we have an interface with optional and default properties in TypeScript?

If you want to set the properties of an interface to have a default value of undefined , you can simply make the properties optional. Copied!

What is unknown TypeScript?

unknown is the type-safe counterpart of any . Anything is assignable to unknown , but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

What is the TypeScript type for object?

In TypeScript, object is the type of all non-primitive values (primitive values are undefined , null , booleans, numbers, bigints, strings). With this type, we can't access any properties of a value.

Are TypeScript interfaces objects?

TypeScript Interface TypeTypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. To create an interface, use the interface keyword followed by the interface name and the typed object.


2 Answers

It's not exactly what you want, but you can use a union type:

interface IfcObject {     fixedKey1: string     fixedKey2: string     [key: string]: string | {         param1: number[];         param2: string;          param3: string;     } } 

It covers your case. But the unknown properties could be of type string.

like image 188
Paleo Avatar answered Sep 30 '22 01:09

Paleo


export interface IfcObjectValues {     param1: number[];     param2: string;     param3: string;         }  interface MyInterface {   fixedKey1: string,   fixedKey2: number,   [x: string]: IfcObjectValues,  } 

Your code in action, see here.

like image 41
bersling Avatar answered Sep 30 '22 03:09

bersling