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

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

Closed
falsandtru opened this issue Jul 30, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@falsandtru
Copy link
Contributor

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.
@RyanCavanaugh
Copy link
Member

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

@falsandtru
Copy link
Contributor Author

I understand as a breaking change, not a bug. Thanks @RyanCavanaugh .

@ahejlsberg
Copy link
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.

@ahejlsberg ahejlsberg reopened this Jul 30, 2016
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Jul 30, 2016
@falsandtru
Copy link
Contributor Author

Thanks for the fix!

@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0.1 milestone Aug 1, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 1, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants