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

bug: type predicates (V is T) fails on expressions (V[K] is T) #12549

Closed
KiaraGrouwstra opened this issue Nov 29, 2016 · 6 comments
Closed

bug: type predicates (V is T) fails on expressions (V[K] is T) #12549

KiaraGrouwstra opened this issue Nov 29, 2016 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@KiaraGrouwstra
Copy link
Contributor

TypeScript Version: nightly (2.2.0-dev.20161128)

Code

propIs<T, V, K extends keyof V>(type: T, name: K, obj: V): V[K] is T;

Expected behavior:
TS being totally cool with V[K] is T.

Actual behavior:
Nope, many syntax errors.

@DanielRosenwasser
Copy link
Member

The type predicate syntax has always taken a parameter name on the left side of the is keyword. So it sounds like what you want is better parser recovery here.

@KiaraGrouwstra
Copy link
Contributor Author

Oh, yeah, V is T itself works fine, this seems to be specifically about adding the [K] there. It does seem like storing (extends) V[K] in a new generic may form a solution here...
But apparently propIs<T, V, K extends keyof V, R extends V[K]>(type: T, name: K, obj: V): R is T; yields Cannot find parameter 'R'. I must be doing something wrong...

@DanielRosenwasser
Copy link
Member

Well it's only the parameter that a type predicate can operate on - in other words, you write

function foo(x: any): x is string {
    // ....
}

As opposed to the following:

function foo(x: any): any is string {
    // ....
}

@KiaraGrouwstra
Copy link
Contributor Author

I'm sorry, that would be my misinterpretation then.
In that event I would be inclined to try the following for this specific case:

propIs<T, V, K extends keyof V, R extends V[K]>(type: T, name: K, obj: V): obj[name] is T;

Still not okay though. I suppose it'd help if I had the ability to save this result to a new variable first, but I'm not confident I can do this in the type language...

@DanielRosenwasser
Copy link
Member

You could do something like this:

propIs<T, V, K extends keyof V, R extends V[K]>(type: T, name: K, obj: V): obj is (V & Record<K, T>)

@KiaraGrouwstra
Copy link
Contributor Author

Whoa, some out of the box thinking there, wouldn't have come up with that myself. Thanks for thinking along. :)

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Nov 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants