I have the following class, for example:
export class User {
public name: string | null;
public birthDate: Date;
}
How can I check, for example, in a component, if name
is a nullable property? I can get its type (string) but I can't get any information regardless if it can be nullable or not.
you can do it like this:
type IsUndefinable<T, TypeIfTrue, TypeIfFalse> = undefined extends T
? TypeIfTrue
: TypeIfFalse;
type IsNullable<T, TypeIfTrue, TypeIfFalse> = null extends T
? TypeIfTrue
: TypeIfFalse;
type IsUserNameUndefinable = IsUndefinable<User["name"], true, false>;
type IsUserNameNullable = IsNullable<User["name"], true, false>;
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