Skip to content

undefined satisfies TestType = 'blah' | number in certain situations, but not others #33787

Closed
@yay

Description

@yay

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

npx tsc --version: Version 3.7.0-dev.20191003

Code
Run with npx tsc index.ts --out index.js --noImplicitAny --strictNullChecks

type TestType = 'blah' | number;
const testObjects = [
    {test: undefined, str: 'foo'},
    {test: 42, str: 'bar'}
] as {test: TestType, str: string}[]; // Why is this not an error?
const obj1: TestType = undefined; // Type 'undefined' is not assignable to type 'TestType'.
const obj2 = undefined as TestType; // Conversion of type 'undefined' to type 'TestType' may be a mistake because neither type sufficiently overlaps with the other.

Expected behavior:
This should be an error

const testObjects = [
    {test: undefined, str: 'foo'},
    {test: 42, str: 'bar'}
] as {test: TestType, str: string}[];

Something in the lines of Conversion of type 'type1' to type 'type2' may be a mistake because neither type sufficiently overlaps with the other.

Actual behavior:
This compiles without errors:

const testObjects = [
    {test: undefined, str: 'foo'},
    {test: 42, str: 'bar'}
] as {test: TestType, str: string}[];

Playground Link:

https://www.typescriptlang.org/play/?ts=3.7-Beta#code/C4TwDgpgBAKhDOwbmgXigcgEYBsCGAFhlAD5QB2ArgLZYQBOA3AFADGA9uYlMAsAPJYAVhFbB4UdAG1mUOVADevRAC4olcgBMIAMwCW5CJoA0URPTUYd7dhgC+x2fKV81AFgBMp85ax569swAulB4Ei6qsHzIkN7AFmbxBgDmdlJBjFAA9FlQAOoEIFB6EsAEJRTswKHkUAz07PQA-Gyc3OzCAIxqcIgxaOpaugZGmTmwKJga2vqGmsQV5FWh8PB6yeR4uNDA7DyTGL1IKBgAdK1c1R1CHpKDMyOaK1F9KGO5AMKcAG4Ma5xQdg6faQKZDWZGYi7EHQQ7RE5Qah4Ip0UKIkrAPAAa2gdFYeEo8Gghj0ZQYMLMlB0+lYegg5GAOCK7F+9HwYAkAHdSQQeARoFV+fRzkA

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