Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: treat null and undefined as the same in strictNullChecks mode #13166

Closed
k8w opened this issue Dec 26, 2016 · 3 comments
Closed

Suggestion: treat null and undefined as the same in strictNullChecks mode #13166

k8w opened this issue Dec 26, 2016 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@k8w
Copy link

k8w commented Dec 26, 2016

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
2.1.4

Suggestion

Sometimes we want to enable strictNullChecks, because null will always with exception, which would interupt our program.

But always people just want to make sure if a variable have been assigned.

For example:

let a:{
  b:{
    c: string;
  }
};
console.log(a.b.c);  //Exception, can be found with strictNullChecks

In another hands, we may really don't care whether a variable without value is null or undefined.
While strictNullChecks treat null and undefined differently.

It bring some inconvenience, like:

interface A{
    b?: {
        c: {
            d: string;
        }
    }
}

let a:A = {}
a.b = null; //error TS2322: Type 'null' is not assignable to type '{ c: { d: string; }; } | undefined'.

or

interface A{
    b: {
        c: {
            d: string
        }
    } | null
}

let a:A = {} //error TS2322: Type '{}' is not assignable to type 'A'. Property 'b' is missing in type '{}'

So if we want to use strictNullChecks to check value have been assigned a value, we may have to:

  • Type a long type def: XXX | null | undefined
  • Remember it is | null or | undefined

Maybe at most time we want to treat null and undefined as the same.
So maybe better if there would be an option to achieve that.

@DanielRosenwasser DanielRosenwasser added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Dec 26, 2016
@DanielRosenwasser
Copy link
Member

I don't think we're willing to make undefined and null each an alias for null | undefined. Having worked with languages that make them out to be the same, it is a little frustrating and can force people into specific coding styles which they may not be fans of.

There's always the option of defining a custom type alias:

type Nullable<T> = T | null | undefined;

@k8w
Copy link
Author

k8w commented Dec 27, 2016

Agree, we should not make undefined and null each an alias for null | undefined.

But what if make T? as an alias for null | undefined?

People always make optional field by T?, most time null is a proper value to them.

May null | undefined is more suitable for optional field?

It also seems Nullable<T> is equals to T? in C#.

@RyanCavanaugh
Copy link
Member

@twoeo see #7426

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants