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?
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:
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.
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 ).
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With