-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Experiment: report functions with multiple returns that could be type predicates #58173
Conversation
@typescript-bot test it |
Hey @RyanCavanaugh, the results of running the DT tests are ready. Everything looks the same! |
@RyanCavanaugh Here are the results of running the user tests comparing Something interesting changed - please have a look. Details
|
@RyanCavanaugh Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
The pyright one is: /~https://github.com/microsoft/pyright/blob/92f2284d2459fed8a9fdaab023527d8598749fcc/packages/pyright-internal/src/analyzer/parseTreeUtils.ts#L2588 Basically the same as |
@RyanCavanaugh Here are the results of running the top 400 repos comparing Something interesting changed - please have a look. Details
|
I feel like it's telling that we're seeing so few of these, and that when we do, the code still compiles as it did before. That makes it seem like this isn't very valuable, and that the initial design was good enough. |
Closing this out since we've learned what we're going to from it. |
BTW thank you for sending these PRs in exactly the format we need to let the infra report something, super helpful. |
This is a side experiment for #58154. That PR extends type predicate inference to functions with multiple
return
s. It seems to have minimal perf impact and also minimal breakages. This makes you wonder… are there any such functions?This fork of the PR should answer that question by reporting errors on all functions with 2+
return
statements where it can infer a type predicate. To be clear, these aren't real errors! They're just identifying multi-return predicates in the wild.There are three such function in TypeScript itself:
tryAddPropertyAssignment
innodeFactory.ts
:This gets an inferred return type of
expression is Expression
. This is accurate, though not consequential for any of the code that calls this function.charIsPunctuation
inpatternMatcher.ts
:This gets an inferred return type of
ch is CharacterCodes._ | CharacterCodes.ampersand | CharacterCodes.asterisk | ... | CharacterCodes.slash
. Again, this is accurate but not consequential for any of the calling code.getLinkedEditingRangeAtPosition
inservices.ts
:The arrow function becomes a type predicate with #58154 which changes the type of
tag
. This is a positive change that eliminates the need for theDebug.assert
statement.Of course, the function could have been written more succinctly as
n => isJsxOpeningElement(n) || isJsxClosingElement(n)
and the existing code would infer a type predicate.I'm curious what other errors this reports on the user/top/etc suites. This will characterize whether this is a common pattern in the wild.