Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer type of tuple from single elements #15484

Closed
phra opened this issue Apr 30, 2017 · 6 comments
Closed

infer type of tuple from single elements #15484

phra opened this issue Apr 30, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@phra
Copy link

phra commented Apr 30, 2017

hi,

while working on await-to-ts i discovered that i cannot publish the to function with the return type of Promise<[Error, null] | [null, T]> but only with Promise<[Error, T]> because it cannot infer correctly the tuple type on partial informations on elements. you can find a live example of this in this branch
schermata da 2017-05-01 00-35-42

TypeScript Version: 2.3.1

Code
playground link

function f(flag: boolean): [number] | [string] {
	if (flag) {
		return [0]
	}

	return ['0']
}

const res = f(true)[0]
if (typeof res === 'number') {
	res // is a number, it works correctly
} else {
	res // is a string, it works correctly
}

const res2 = f(true)
if (typeof res2[0] === 'number') {
	const tmp1 = res[0] // is string | number, it should be number
} else {
	res // is a string, it works correctly
}


function g(flag: boolean): [number, string] | [string, number] {
	if (flag) {
		return [0, '0']
	}

	return ['0', 0]
}

const res1 = g(true)
if (typeof res1[0] === 'number') {
	const tmp1 = res1[0] // is string | number, it should be number
	const tmp2 = res1[1] // is string | number, it should be string
} else {
	const tmp1 = res1[0] // is string | number, it shuold be string
	const tmp2 = res1[1] // is string | number, it should be number
}

Expected behavior:
based on partial informations, the type system should be able to infer tuple type correctly.
Actual behavior:
the type system fails to infer tuple types with partial informations about elements.

@jurajkocan
Copy link

suggestion workaround with objects instead tuples: 15482

@kitsonk
Copy link
Contributor

kitsonk commented May 1, 2017

Yeah, I still think fixing this or #15482. TypeScript could narrow these types. Certainly related though.

@phra
Copy link
Author

phra commented May 1, 2017

i think that it should be a killing feature because in my case i can use the type system to signal to the user that is trying to access the resolved value of the promise without checking the error before, because if you don't check the err variable the resolved value will have a null | T type, after checking the error the type will be correctly T.
@jurajkocan i was thinking about that, maybe i can give it a try and see, but the tuple syntax is for sure more elegant in this case.

@phra
Copy link
Author

phra commented May 4, 2017

no plan to see this FR approved soon?

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2017

Duplicate of #12849

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 5, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed May 22, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants