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

RegExpMatchArray has lost some compatibility with Array since 2.1.0-dev.20160729 #10041

Description

TypeScript Version: master

Code

(''.match(/ /) || []).map(s => s.toLowerCase());

Expected behavior:

$ node built/local/tsc.js --lib es6 --strictNullChecks index.ts

Actual behavior:

$ node built/local/tsc.js --lib es6 --strictNullChecks index.ts
index.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature.

Activity

  1. RyanCavanaugh commented on Jul 30, 2016

    @RyanCavanaugh
    Member

    Same root cause as #10025 but it's much less clear how to fix this example

  2. falsandtru commented on Jul 30, 2016

    @falsandtru
    ContributorAuthor

    I understand as a breaking change, not a bug. Thanks Ryan Cavanaugh (@RyanCavanaugh) .

  3. ahejlsberg commented on Jul 30, 2016

    @ahejlsberg
    Member

    Several subtle issues at play here. With #9407 we still perform subtype reduction for the operands of the || and ?: operators. However, even after subtype reduction the expression (''.match(/ /) || []) has type RegExpMatchArray | never[] because there are extra optional properties introduced by RegExpMatchArray. In the property access for the map property we would previously perform subtype reduction again, and that would reduce the union function type of map down to a single type. This step we now omit because of #9407.

    I think the real issue here is that we produce the union type RegExpMatchArray | [] instead of just RegExpMatchArray (and this issue was there all along). It may be that for || and ?: we should say that if neither operand is a subtype of the other, but one is assignable to the other, we pick the type of the one to which the other is assignable.

  4. falsandtru commented on Jul 30, 2016

    @falsandtru
    ContributorAuthor

    Thanks for the fix!

  5. added this to the milestone on Aug 1, 2016
  6. 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

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions