Answer the question
In order to leave comments, you need to log in
How to use null check in TypeScript in function instead of if?
It is easier to explain the question with a simple example
const isNotEmptyString = (str: string|null): boolean => {
if (!str) {
return false;
}
return !!str.length;
};
const getStringLength = (str: string|null): number => {
if (!isNotEmptyString(str)) {
return 0;
}
return str.length; // Qualifier of 'length' is possibly null
};
Answer the question
In order to leave comments, you need to log in
can. it's called type-predicate
const isNotEmptyString = (str: string|null): str is string => { ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question