'Typescript guard generic
object may be {payload: {name: ''}} or {payload: {age: 11}}, how to declare a function param?
type AnyType<T> = {
payload: T;
}
type A = {
name: string;
};
type B = {
age: number;
};
function getValue(params: AnyType<unknown>) {
// here error occurred
if (typeof (params.payload) === A) {
// log(params.payload.name)
}
if (typeof params.payload === B) {
// log(params.payload.age)
}
}
How to guard A or B in function?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
