For :
abc:number|string;
Use the JavaScript operator typeof:
if (typeof abc === "number") {
// do something
}
TypeScript understands typeof 🌹
This is called a typeguard.
More
For classes you would use instanceof e.g.
class Foo {}
class Bar {}
// Later
if (fooOrBar instanceof Foo){
// TypeScript now knows that `fooOrBar` is `Foo`
}
There are also other type guards e.g. in etc https://basarat.gitbook.io/typescript/type-system/typeguard
For :
abc:number|string;
Use the JavaScript operator typeof:
if (typeof abc === "number") {
// do something
}
TypeScript understands typeof 🌹
This is called a typeguard.
More
For classes you would use instanceof e.g.
class Foo {}
class Bar {}
// Later
if (fooOrBar instanceof Foo){
// TypeScript now knows that `fooOrBar` is `Foo`
}
There are also other type guards e.g. in etc https://basarat.gitbook.io/typescript/type-system/typeguard
The other answers are right, but when you're dealing with interfaces you cannot use typeof or instanceof because interfaces don't get compiled to javascript.
Instead you can use a typecast + function check typeguard to check your variable:
interface Car {
drive(): void;
honkTheHorn(): void;
}
interface Bike {
drive(): void;
ringTheBell(): void;
}
function start(vehicle: Bike | Car ) {
vehicle.drive();
// typecast and check if the function exists
if ((<Bike>vehicle).ringTheBell) {
const bike = (<Bike>vehicle);
bike.ringTheBell();
} else {
const car = (<Car>vehicle);
car.honkTheHorn();
}
}
And this is the compiled JavaScript in ES2017:
function start(vehicle) {
vehicle.drive();
if (vehicle.ringTheBell) {
const bike = vehicle;
bike.ringTheBell();
}
else {
const car = vehicle;
car.honkTheHorn();
}
}
Getting type of a property of a TypeScript class using keyof operator - Stack Overflow
Getting the exact type of an object
How to get type from array of objects
Create a type based on value in object
public random(request: a | b){
let getValueA ; let getValueB;
If (request is of type A) { getValueA = request.propertyInA_Only; } Else { getValueB = request.propertyInB_Only;
}
Can anybody help with this? How can i get the type of request? A is interface B is class
Yes, lookup types work just fine:
type BarType = FooType['bar'];
It expects in this case that FooType is an object like:
type FooType = {
bar: string;
}
It sets BarType to the same type as FooType['bar'], so to a string.
PS: FooType can also be an interface or class.
Is this what you're looking for?
type PropType<TObj, TProp extends keyof TObj> = TObj[TProp];
and get type of an object property by doing:
type MyPropType = PropType<ObjType, '<key>'>;
which is the same as the way of using Pick in typescript, and it can report compile error if there's any invalid key passed in.
Updates
As @astoilkov suggested, a simpler alternative is PropType['key'].
type myType = "a" | "b"; const myVariable: myType = "a"; type myVariableType = typeof myVariable; // the type is myType, but I want it to be "a" explicitly
Is there a way I can get the exact type of an object while not removing the type annotation (: myType)?