6

This might be a silly question, but I can't figure out how to set a value of null to variable declared to be of an interface type.

Below is an example of what I mean:

interface IMyInterface {
    field: any;
}

let test:IMyInterface = null; // <-- Compiler error: Type 'null' is not assignable to type 'IMyInterface'
4
  • 1
    let test:IMyInterface | null = null; Commented Jun 25, 2020 at 13:36
  • And what if that interface is and argument of a function I can't change? Specifically I'm trying to pass null to the default value of react createContext Commented Jun 25, 2020 at 13:40
  • You would get solutions to the real issue much quicker if you posted a question that has your actual code and problem description instead of a question that's too abstract to solve the actual issue. Commented Jun 25, 2020 at 13:43
  • You are absolutely right. My apologies, I thought the issue I had was related to soming I was missing with the declaration of my interface. Commented Jun 25, 2020 at 13:45

3 Answers 3

9

Either define it to be an union type, or set the strictNullChecks flag to false in your ts-config under compilerOptions.

by @Guy Incognito:

let test:IMyInterface | null = null;
Sign up to request clarification or add additional context in comments.

Comments

6

Enabling the compiler setting strictNullChecks (which is also included in the strict setting) removes null and undefined from each type and thus requires these to be explicitly declared. TypeScript handbook on Nullable types.

Without strictNullChecks:

declare let implicitlyNullable: number;

implicitlyNullable = 42;        //OK
implicitlyNullable = null;      //OK
implicitlyNullable = undefined; //OK

Playground Link

With strictNullChecks:

declare let implicitlyNullable: number;

implicitlyNullable = 42;        //OK
implicitlyNullable = null;      //error
implicitlyNullable = undefined; //error

///////////////////////////////////////

declare let explicitlyNullable: number | null;

explicitlyNullable = 42;        //OK
explicitlyNullable = null;      //OK
explicitlyNullable = undefined; //error

///////////////////////////////////////

declare let explicitlyUndefinable: number | undefined;

explicitlyUndefinable = 42;        //OK
explicitlyUndefinable = null;      //error
explicitlyUndefinable = undefined; //OK

///////////////////////////////////////

//nilable = can be both undefined and null
declare let explicitlyNilable: number | undefined | null;

explicitlyNilable = 42;        //OK
explicitlyNilable = null;      //OK
explicitlyNilable = undefined; //OK

Playground Link


In essence, the compiler option is very similar to automatically applying the NonNullable utility type to every type in existence. So, without strictNullChecks being you don't accept "empty" values is an opt-in.

Speaking for myself, I much prefer the exactness of strictNullChecks. It makes it much easier to write and reason about code without having to sprinkle if(someVar) everywhere because you don't know what somebody would pass in. Even a simple function can be a problem without strict null checks:

function add(a: number, b: number): number {
    return a + b;
}

console.log(add(2, 3));         //strictNullChecks: false -> 5
                                //strictNullChecks: true  -> 5
console.log(add(2, null));      //strictNullChecks: false -> 2
                                //strictNullChecks: true  -> compiler error
console.log(add(2, undefined)); //strictNullChecks: false -> NaN
                                //strictNullChecks: true  -> compiler error

Playground Link - strictNullChecks: false

Playground Link - strictNullChecks: true


For convenience, you can create some generic types that handle different types of nullability:

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

declare let implicitlyNullable: number;
declare let explicitlyNullable: Nullable<number>;
declare let explicitlyUndefinable: Undefinable<number>;
declare let explicitlyNilable: Nilable<number>;

Playground Link

Comments

-1
let test:IMyInterface = null as unknown as IMyInterface;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.