Skip to content

Incorrect tuple type narrowing #23512

Closed
Closed
@ccorcos

Description

@ccorcos

TypeScript Version: 2.8.1

Search Terms: Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple

Code

type x = [string]
type y = [string, number]
type z = x | y

function f(arg: z) {
	if (arg[1] !== undefined) {
		const result = arg      // z; should be y
		const first = result[1] // string | number; should be number
		return first
	} else {
		return undefined
	}
}

Expected behavior:

I would expect the type system to narrow the types based on unions of tuples.

Actual behavior:

Doesn't happen.

Playground Link:

http://www.typescriptlang.org/play/#src=type%20x%20%3D%20%5Bstring%5D%0D%0Atype%20y%20%3D%20%5Bstring%2C%20number%5D%0D%0Atype%20z%20%3D%20x%20%7C%20y%0D%0A%0D%0Afunction%20f(arg%3A%20z)%20%7B%0D%0A%09if%20(arg%5B1%5D%20!%3D%3D%20undefined)%20%7B%0D%0A%09%09const%20result%20%3D%20arg%20%20%20%20%20%20%2F%2F%20z%3B%20should%20be%20y%0D%0A%09%09const%20first%20%3D%20result%5B1%5D%20%2F%2F%20string%20%7C%20number%3B%20should%20be%20number%0D%0A%09%09return%20first%0D%0A%09%7D%20else%20%7B%0D%0A%09%09return%20undefined%0D%0A%09%7D%0D%0A%7D

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions