In TypeScript 2, you can use the undefined type to check for undefined values.
If you declare a variable as:
let uemail : string | undefined;
Then you can check if the variable uemail is undefined like this:
if(uemail === undefined)
{
}
Answer from ashish on Stack OverflowIn TypeScript 2, you can use the undefined type to check for undefined values.
If you declare a variable as:
let uemail : string | undefined;
Then you can check if the variable uemail is undefined like this:
if(uemail === undefined)
{
}
From Typescript 3.7 on, you can also use nullish coalescing:
let x = foo ?? bar();
Which is the equivalent for checking for null or undefined:
let x = (foo !== null && foo !== undefined) ?
foo :
bar();
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing
While not exactly the same, you could write your code as:
var uemail = localStorage.getItem("useremail") ?? alert('Undefined');
Videos
What you can do is check whether you props is defined initially or not by checking if nextProps.blog.content is undefined or not since your body is nested inside it like
componentWillReceiveProps(nextProps) {
if(nextProps.blog.content !== undefined && nextProps.blog.title !== undefined) {
console.log("new title is", nextProps.blog.title);
console.log("new body content is", nextProps.blog.content["body"]);
this.setState({
title: nextProps.blog.title,
body: nextProps.blog.content["body"]
})
}
}
You need not use type to check for undefined, just the strict operator !== which compares the value by their type as well as value
In order to check for undefined, you can also use the typeof operator like
typeof nextProps.blog.content != "undefined"
I was face same problem ..... And I got solution by using typeof()
if (typeof(value) !== 'undefined' && value != null) {
console.log('Not Undefined and Not Null')
} else {
console.log('Undefined or Null')
}
You must have to use typeof() to identified undefined
Hello. I have a simple question, most likely some rookie mistake I am making, but not exactly sure why it's happening.
I have a simple interface IPerson, a person object, and finally a function searchPerson that lets you search a person's hobbies if they have any.
TypeScript Playground link
interface IPerson {
skills?: {
name: string;
}[];
hobbies?: {
name: string;
}[];
}
const person: IPerson = {
hobbies: [
{ name: 'programming' },
{ name: 'gaming' },
]
};
const searchPerson = ({ type, search }: { type: keyof IPerson, search: string }) => {
if (type === 'hobbies') {
if (person[type]) {
person[type].find(hobby => hobby.name === search); // <= error is here
}
}
};
All seems to work as intended, except for the fact that Typescript is complaining that Object is possibly 'undefined'. at the end of my function at the person[type].find(...) part, even though just the line above I am checking if if (person[type) is not undefined.
In the past I have dealt with something similar. For example, instead of keyof IPerson as type parameter of the function, I was using an arbitrary string which was causing issues, but here I am clearly telling TypeScript that type should be a key of IPerson, and since hobbies is an optional parameter I am checking if (person[type]) exists first. Yet it's still telling me that it's possibly undefined.
I am kinda confused, anyone has an idea what is happening here or can point me in the right direction? Cheers!
EDIT: Forgot to mention, I am aware that I can use optional chaining (person[type]?.find(...)) or non-null assertion operator (person[type]!.find(...)) but I am trying to understand why this is treated as undefined, rather than looking for a workaround.
Try first assigning person[type] to a variable, then do the condition and the find on that variable. That should work. I remember reading a while back that typescript does this for safety because it cannot statically determine the difference between a getter and a property so what is truthy on one line may no longer be on the next if it's a getter.
It’s possible to construct an object that implements the IPerson interface but returns a different value each time you access person[“hobbies”]. So, the compiler doesn’t know that the first result of person[type] being defined means the second result will be as well.
Therefore, you need to access it once and store it, then write your type guard against the stored value:
const hobbies = person[type]
if (hobbies) {
hobbies.find(…)
}
Hello guys, i need your help, i would like to know which you think is a better approach:
1 --------------------------------------------
const {content} = props;
if(content !== "undefined"){
//code
}
2 --------------------------------------------
const {content = null} = props;
if(!content){
//code
}
This code do not work at all, but not sure why.
type IsUndefined<T> = T extends undefined ? 1 : 0;
For example:
type IsTypeUndefined = IsUndefined<number | undefined>; // This returns: 1 | 0
Is there a way for check if a type is undefined in a conditional?
Update: For simplicity, I replicated the original error with simpler code.
Update 2: I got around this by using a type guard for T. Thank u/ritajalilip for the suggestion. I still think Typescript should handle this better though.
As you can see on the screenshot, inside the if statement I'm asserting that x is not undefined. However Typescript is failing to acknowledge and still thinks that x can be undefined.
Any idea of what's going on?
Using a juggling-check, you can test both null and undefined in one hit:
if (x == null) {
If you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:
if (x === null) {
You can try this with various values using this example:
var a: number;
var b: number = null;
function check(x, name) {
if (x == null) {
console.log(name + ' == null');
}
if (x === null) {
console.log(name + ' === null');
}
if (typeof x === 'undefined') {
console.log(name + ' is undefined');
}
}
check(a, 'a');
check(b, 'b');
Output
"a == null"
"a is undefined"
"b == null"
"b === null"
if( value ) {
}
will evaluate to true if value is not:
nullundefinedNaN- empty string
'' 0false
typescript includes javascript rules.