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

Narrowing union types with array's indexOf #9842

Closed
normalser opened this issue Jul 20, 2016 · 1 comment
Closed

Narrowing union types with array's indexOf #9842

normalser opened this issue Jul 20, 2016 · 1 comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@normalser
Copy link

normalser commented Jul 20, 2016

It would be great if TS could handle this:

                if (typeof reply == 'string') {

                }
                else if (~['input', 'button'].indexOf(reply.typeName)) {
                    const test = reply // Input or Button - currently everything
                }
@DanielRosenwasser
Copy link
Member

There's a lot going on there that would need to be formalized. Specifically:

  1. The expression of a property access becomes a literal type location.
  2. An array literal becomes a literal type location if its parent is a literal type location.
  3. indexOf would need to have a way of expressing that any value >= 0 implies that its type is related to its array's numeric index signature.

(1) and (2) are very easy to implement, but I think that (3) is too complex to express right now. While we have type predicate functions, we'd need a more powerful mechanism for expressing relationships between produced values and their arguments.

@mhegazy mhegazy added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Sep 29, 2016
@mhegazy mhegazy closed this as completed Sep 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
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

3 participants