Skip to content

TS2322: Type 'null' is not assignable to type 'string | void' #8322

@jovdb

Description

@jovdb

TypeScript Version: nightly (1.9.0-dev.20160426)
EDIT: used flags: --strictNullChecks --noImplicitAny --noImplicitThis

const a1: string | void = "a";
const a2: string | void = undefined;
const a3: string | void = null; // error TS2322: Type 'null' is not assignable to type 'string | void'.

The documentation says that you can assign undefined or null to void:

Declaring variables of type void is not useful because you can only assign undefined or null to them

Is the documentation still correct?
It looks like we cannot assign null to void, so is void the same as undefined?

A related problem in my type guard function (simular to TS v1.8 sample):

function isVoid<T>(obj: T | void): obj is void {
  return obj === null || obj === undefined; // error TS2365: Operator '===' cannot be applied to types 'T | void' and 'null'.
  // return obj === undefined; // Is OK
}

Can someone clarify the differences/relations between: void, null, undefined and maybe also {} and any in TS v1.9+?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions