typescript get type of object code example
Example 1: typescript type object
type Dictionary = {
[key: string]: any
}
Example 2: typescript check type of variable
if (fooOrBar instanceof Foo){
}
Example 3: typescript get type
if (typeof abc === "number") {
}
Example 4: typescript get type of object property
type Data = {
value: number;
text: string;
};
type textProperty = Data["text"];
const data = {
value: 123,
text: 'hello'
};
type textProperty = typeof data["text"];
Example 5: object type in typescript
declare function create(o: object | null): void;
create(42);Argument of type '42' is not assignable to parameter of type 'object | null'.2345Argument of type '42' is not assignable to parameter of type 'object | null'.create("string");Argument of type '"string"' is not assignable to parameter of type 'object | null'.2345Argument of type '"string"' is not assignable to parameter of type 'object | null'.create(false);Argument of type 'false' is not assignable to parameter of type 'object | null'.2345Argument of type 'false' is not assignable to parameter of type 'object | null'.Try
Example 6: typescript type of object values
const data = {
value: 123,
text: 'text'
};
type Data = typeof data;