镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Narrowing union types with array's indexOf #9842

Description

@normalser

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
                }

Activity

  1. DanielRosenwasser commented on Jul 20, 2016

    @DanielRosenwasser
    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.

  2. added
    SuggestionAn idea for TypeScript
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    on Sep 29, 2016
  3. locked and limited conversation to collaborators on Jun 19, 2018
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

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions