Skip to content

Union type narrowing with generics not working in TS 3.5 RC strict mode #31456

Description

TypeScript Version: 3.5.0-rc / next

Search Terms: 3.5, 3.5 union, 3.5 strict

Code

test.ts:

functionfooT<T=string>(bar: T[]|T|undefined){if(bar!==undefined&&!Array.isArray(bar))thrownewError("expected array")elseif(bar!==undefined&&bar.length===0)console.log("empty array")elseconsole.log("array or undef.")}

CLI:

npx typescript@3.4.5 --strict true test.ts // compiles
npx typescript@next --strict true test.ts // error

Expected behavior:
Compiles and behaves like ts 3.4.5

Actual behavior:
Error:

test.ts:4:37 - error TS2339: Property 'length' does not exist on type 'T | T[]'.
Property 'length' does not exist on type 'T'.

Related Issues:
Cannot judge, if it is a duplicate of #31380 .
This is some specific case to base further evaluation on.

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