Skip to content

Incorrect tuple type narrowing #23512

Closed
Closed

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:

Activity

  1. ccorcos commented on Apr 18, 2018

    @ccorcos
    Author

    I should also mention that this also doesn't work for "tagged" tuple unions either.

    type x = ["x"]
    type y = ["y", number]
    type z = x | y
    
    function f(arg: z) {
    	if (arg[0] === "x") {
    		const result = arg      // z; should be y
    		const first = result[1] // string | number; should be number
    		return first
    	} else {
    		return undefined
    	}
    }
  2. ccorcos commented on Apr 18, 2018

    @ccorcos
    Author

    Bonus: I could be wrong, but if you could do this and were to use enums, you basically have GADTs in Typescript 👍

  3. ccorcos commented on Apr 18, 2018

    @ccorcos
    Author

    I'm creating some functions to work around this issue and I'm getting some issues using function overloads as well.

    type w = ["w", string]
    type x = ["x"]
    type y = ["y", number]
    type z = w | x | y
    
    function f(arg: w): string
    function f(arg: x): undefined
    function f(arg: y): number
    function f(arg: z): string | number | undefined {
    	if (arg[1] !== undefined) {
    		return arg[1]
    	}
    }
    
    const a = f(["w", "hello"])
    const b = f(["x"])
    const c = f(["y", 10])
    
    let value: z | undefined
    if (value) {
    	const data = f(value) // Doesn't work.
    }
  4. ccorcos commented on Apr 18, 2018

    @ccorcos
    Author

    Similar issue, but this is more precisely the one I'm dealing with in my code.

    Types of property 'length' are incompatible.

    type w = ["w", string]
    type x = [string]
    type z = w | x
    
    function f(arg: w): string
    function f(arg: x): undefined
    function f(arg: z): string | number | undefined {
    	if (arg[1] !== undefined) {
    		return arg[1]
    	}
    }
    
    const a = f(["w", "hello"])
    const b = f(["x"])
    
    let value: z | undefined
    if (value) {
    	const data = f(value) // Doesn't work.
    }
  5. RyanCavanaugh commented on Aug 22, 2018

    @RyanCavanaugh
    Member

    You can do this now by testing for arg.length === 2

    We could potentially add e.g. arr[7] === undefined as a special type guard for tuples. Is checking the length instead a good solution for you?

  6. ccorcos commented on Aug 22, 2018

    @ccorcos
    Author

    Nice, but that won't cover a situation where you have multiple tuples of the same length. It also doesn't work with a simply typeof check.

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

    playground

  7. RyanCavanaugh commented on Aug 22, 2018

    @RyanCavanaugh
    Member

    Actually #26424 should fix this

  8. RyanCavanaugh commented on Aug 22, 2018

    @RyanCavanaugh
    Member
  9. nikeee commented on Apr 3, 2020

    @nikeee
    Contributor

    I have a silimar related issue. Is there any reason TS cannot narrow the tuple types in this case?

    type S = [string];
    type N = [number];
    
    function fn(param: S | N) {
      if (typeof param[0] === 'number') {
        param; // S | N; expected N
      } else {
        param; // S | N; expected S
      }
    }

    Tested in playground's "Nightly" compiler.

  10. 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

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