Skip to content

TS2322: Invalid assignment error #33135

@SetTrend

Description

@SetTrend

TypeScript Version: 3.6.2

Search Terms: TS2322 Invalid assignment error

Code

TypeScript assignment bug

{
    "compilerOptions": {
        "outDir": "dist",
        "allowJs": true,
        "checkJs": true,
        "downlevelIteration": true,
        "emitBOM": true,
        "forceConsistentCasingInFileNames": true,
        "noImplicitAny": true,
        "module": "ESNext",
        "noEmitOnError": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "strict": true,
        "target": "es5",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "moduleResolution": "node"
    },
    "include": [
        "./app/",
        "./scripts/"
    ]
}

Expected behavior:

The assignment should be valid, because both sides of the assignment as declared as string | null.

Actual behavior:

TypeScript raises the error message:

TS2322: Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.

Playground Link: Sorry, no playground link. I don't know nor do I understand what causes the false behaviour on this particular assignment.

Related Issues: ./.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions