Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - Object with any number of key values

Tags:

typescript

I'm trying writing an interface to figure to handle data like this:

interface SessionList {
   appInfo: Object(string: string | null);
}

There could be 0 to many items under appInfo. I'm trying to find the correct syntax but not luck - currently at:

appInfo: Object(string: string | null);

Any ideas?

like image 711
userMod2 Avatar asked Apr 25 '19 08:04

userMod2


People also ask

What is an object type in typescript?

In JavaScript, the fundamental way that we group and pass around data is through objects. In TypeScript, we represent those through object types. As we’ve seen, they can be anonymous:

How do you define a type with dynamic keys in typescript?

Define a Type for Object with Dynamic keys in TypeScript# Use an index signature to define a type for an object with dynamic keys, e.g. [key: string]: string;. Index signatures are used when we don't know all of the names of a type's properties ahead of time, but know the shape of the values.

What is an anonymous type in typescript?

In TypeScript, we represent those through object types. As we’ve seen, they can be anonymous: or a type alias. In all three examples above, we’ve written functions that take objects that contain the property name (which must be a string) and age (which must be a number ).

What is a key-value pair in typescript?

Typescript comes up with functions that can be used for writing scripts. A key-value pair is a wonderful functionality in an object-oriented programming approach that can be used in Typescript for generating values. These key-value pairs in Typescript are present in the Typescript Object. The values can be function, an array of objects, etc.


1 Answers

Either

interface AppInfo {
  [key: string]: string | null;
}

Or

Record<string, string | null>

Update:

let obj: { appInfo: Record<string, string | null> };

interface SessionList {
   appInfo: Record<string, string | null>;
}

let obj: SessionList;
like image 193
Roberto Zvjerković Avatar answered Sep 30 '22 08:09

Roberto Zvjerković