Skip to content

Apparent properties in union of primitive and object causes incorrect error #31668

Closed

Description

TypeScript Version: 3.5.1

Search Terms: sub blink sup bold HTML Element String.prototype methods

Code

type sign = (payload: string | object) => void;
const sign: sign = (payload) => { console.log(payload)}
sign({ sub: null })

Expected behavior: Ok

Actual behavior:

$ ./node_modules/.bin/tsc --version
Version 3.5.1
$ ./node_modules/.bin/tsc --lib es2015 --strict ./repro.ts
repro.ts:5:6 - error TS2326: Types of property 'sub' are incompatible.
  Type 'null' is not assignable to type '(() => string) | undefined'.

5 sign({ sub: null })
       ~~~~~~~~~~~~~


Found 1 error.
$ ./node_modules/.bin/tsc --version
Version 3.6.0-dev.20190530
$ ./node_modules/.bin/tsc --lib es2015 --strict ./repro.ts
repro.ts:5:6 - error TS2326: Types of property 'sub' are incompatible.
  Type 'null' is not assignable to type '(() => string) | undefined'.

5 sign({ sub: null })
       ~~~~~~~~~~~~~


Found 1 error.

Activity

  1. changed the title [-][3.5.1 Regression] String.prototype methods confusion when string|object[/-] [+]Apparent properties in union of primitive and object causes incorrect error[/+] on May 31, 2019
  2. RyanCavanaugh commented on May 31, 2019

    @RyanCavanaugh
    Member

    Wesley Wigham (@weswigham) reduced

    type Thing = number | object;
    const k: Thing = { toFixed: null };

    Looks like the discriminating logic gets tripped up

  3. weswigham commented on May 31, 2019

    @weswigham
    Member

    Looks like the discriminating logic gets tripped up

    Hm, I guess we probably shouldn't look at apparent types for discriminators when object is involved in the union (since we only want to discriminate against other real object types, not primitive apparent types).

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions