Difference between void, undefined and null in Typescript?

51 views Asked by At
let nothing: void = undefined;
console.log(nothing); // undefined
console.log(typeof nothing); // undefined
// console.log(nothing + 1); // error : Operator '+' cannot be applied to types 'void' and 'number'.ts(2365)

// let nothing2: void = null; // error

let nothing3: any = undefined;
console.log(nothing3 + 1); // NaN

console.log(void + 1); // undefined
console.log(void + "1"); // undefined
// console.log(null + 1); // error
console.log(null + "1"); // null1
// console.log(undefined + 1); // error
console.log(undefined + "1"); // undefined1

Can someone explain these? And void operator return "undefined" but the result of the examples is not very clear. Please do not mark my question as a duplicate of this: i dont ask difference between undefined and null. And these a, b : i dont ask void is or is not an alias for undefined and also contrary to the accepted answer in the question you can not set null to void variable.

  1. What are void, undefined and null type structures?
  2. What are the differences between them?
  3. The only return type in the function is "return;" what does it return?
funcFoo(fooStr: string) {
    let obj: T;

    if ( ) {
        ...
        return;
    }
    else if (...) {
        ...
        return undefined;
    }

    return obj;
}
0

There are 0 answers