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

[ID-Prep] PR3 - Preserve parameter types for optional parameters /fields with undefined in type and for required params with default value #57484

Merged

Conversation

dragomirtitian
Copy link
Contributor

Fixes #57483

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Feb 22, 2024
if (!getParseTreeNode(node)) {
return type ? visitNode(type, visitDeclarationSubtree, isTypeNode) : factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
if (node.kind === SyntaxKind.SetAccessor) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensureType is never called with a SetAccessorDeclaration. The set accessor type does not have a return type and the parameter type is inferred independently. Removing this had 0 impact on tests.

Comment on lines -772 to -773
if (isPropertySignature(node) || !node.initializer) return cleanup(resolver.createTypeOfDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker, shouldUseResolverType));
return cleanup(resolver.createTypeOfDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker, shouldUseResolverType) || resolver.createTypeOfExpression(node.initializer, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is overly complicated. Both calls to createTypeOfDeclaration boil down to the same arguments. Also I have not found a test that benefits from the use of createTypeOfExpression on the initializer and I can't think of a case when this would help. (There might be some error case, but it's not tested for and returning any for that seems just as appropriate.

}
return cleanup(resolver.createReturnTypeOfSignatureDeclaration(node, enclosingDeclaration, declarationEmitNodeBuilderFlags, symbolTracker));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the cleanup closure. Since the logic is simpler now it's easier to just assign a variable and do the same cleanup after the switch.

isRequiredInitializedParameter(node: ParameterDeclaration): boolean;
isOptionalUninitializedParameterProperty(node: ParameterDeclaration): boolean;
requiresAddingImplicitUndefined(node: ParameterDeclaration): boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRequiredInitializedParameter and isOptionalUninitializedParameterProperty were used only once in the declaration transform in (resolver.isRequiredInitializedParameter(node) || resolver.isOptionalUninitializedParameterProperty(node)). I think it's worth simplifying the API and only expose a function taht tells us is the parameter requires adding an implcit undefined to it's type or not.

requiresAddingImplicitUndefined also takes into account if the type already contains undefined and thus avoids re-printing types unless it's absolutely necessary. (Fixing #57483)

@dragomirtitian dragomirtitian force-pushed the verbatim-declarations-optional-params branch from bf1ee1e to 85f60d9 Compare February 22, 2024 18:24
@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Feb 27, 2024
@jakebailey
Copy link
Member

@typescript-bot test top200
@typescript-bot user test this

@typescript-bot perf test this faster
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Heya @jakebailey, I've started to run the diff-based user code test suite on this PR at a05d427. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Heya @jakebailey, I've started to run the tarball bundle task on this PR at a05d427. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Heya @jakebailey, I've started to run the faster perf test suite on this PR at a05d427. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Heya @jakebailey, I've started to run the diff-based top-repos suite on this PR at a05d427. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/160079/artifacts?artifactName=tgz&fileId=FDE817C914166D03968B5E61E6C598FC243A028DFB51618D74A8AD63A70D275E02&fileName=/typescript-5.5.0-insiders.20240227.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.5.0-pr-57484-5".;

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user test suite comparing main and refs/pull/57484/merge:

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Package install failed"

Otherwise...

Something interesting changed - please have a look.

Details

puppeteer

packages/browsers/test/src/tsconfig.json

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,668k (± 0.01%) 295,691k (± 0.01%) ~ 295,656k 295,749k p=0.575 n=6
Parse Time 2.67s (± 0.39%) 2.67s (± 0.21%) ~ 2.66s 2.67s p=0.663 n=6
Bind Time 0.83s (± 0.91%) 0.83s (± 1.47%) ~ 0.82s 0.85s p=0.388 n=6
Check Time 8.26s (± 0.29%) 8.25s (± 0.25%) ~ 8.23s 8.28s p=0.742 n=6
Emit Time 7.10s (± 0.42%) 7.12s (± 0.54%) ~ 7.07s 7.18s p=0.374 n=6
Total Time 18.85s (± 0.22%) 18.86s (± 0.17%) ~ 18.83s 18.92s p=0.329 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 194,503k (± 1.64%) 195,438k (± 1.53%) ~ 191,581k 197,383k p=0.936 n=6
Parse Time 1.35s (± 1.48%) 1.36s (± 1.14%) ~ 1.35s 1.39s p=1.000 n=6
Bind Time 0.72s (± 0.00%) 0.72s (± 0.00%) ~ 0.72s 0.72s p=1.000 n=6
Check Time 9.34s (± 0.13%) 9.35s (± 0.13%) ~ 9.34s 9.37s p=0.061 n=6
Emit Time 2.60s (± 0.53%) 2.61s (± 0.64%) ~ 2.59s 2.63s p=0.618 n=6
Total Time 14.01s (± 0.20%) 14.05s (± 0.09%) +0.04s (+ 0.25%) 14.03s 14.06s p=0.032 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,475k (± 0.01%) 347,475k (± 0.00%) ~ 347,451k 347,498k p=1.000 n=6
Parse Time 2.48s (± 0.57%) 2.48s (± 0.59%) ~ 2.46s 2.50s p=0.870 n=6
Bind Time 0.93s (± 0.44%) 0.93s (± 0.44%) ~ 0.92s 0.93s p=1.000 n=6
Check Time 6.96s (± 0.38%) 6.94s (± 0.20%) ~ 6.92s 6.96s p=0.157 n=6
Emit Time 4.05s (± 0.52%) 4.07s (± 0.40%) ~ 4.04s 4.08s p=0.222 n=6
Total Time 14.42s (± 0.23%) 14.41s (± 0.11%) ~ 14.39s 14.43s p=0.806 n=6
TFS - node (v18.15.0, x64)
Memory used 302,874k (± 0.01%) 302,877k (± 0.01%) ~ 302,846k 302,939k p=0.936 n=6
Parse Time 2.02s (± 0.85%) 2.01s (± 0.80%) ~ 1.99s 2.03s p=0.415 n=6
Bind Time 1.00s (± 0.41%) 1.00s (± 0.81%) ~ 1.00s 1.02s p=1.000 n=6
Check Time 6.35s (± 0.53%) 6.36s (± 0.33%) ~ 6.34s 6.39s p=0.468 n=6
Emit Time 3.60s (± 0.45%) 3.59s (± 0.82%) ~ 3.56s 3.64s p=0.420 n=6
Total Time 12.97s (± 0.34%) 12.97s (± 0.34%) ~ 12.92s 13.05s p=0.936 n=6
material-ui - node (v18.15.0, x64)
Memory used 511,295k (± 0.01%) 511,308k (± 0.01%) ~ 511,268k 511,382k p=0.471 n=6
Parse Time 2.65s (± 0.65%) 2.65s (± 0.39%) ~ 2.63s 2.66s p=0.870 n=6
Bind Time 0.99s (± 0.99%) 0.99s (± 1.04%) ~ 0.98s 1.01s p=0.591 n=6
Check Time 17.23s (± 0.61%) 17.26s (± 0.35%) ~ 17.16s 17.34s p=0.423 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.86s (± 0.54%) 20.91s (± 0.30%) ~ 20.80s 20.98s p=0.296 n=6
mui-docs - node (v18.15.0, x64)
Memory used 2,294,976k (± 0.00%) 2,294,985k (± 0.00%) ~ 2,294,928k 2,295,033k p=1.000 n=6
Parse Time 11.97s (± 0.93%) 12.13s (± 1.64%) ~ 11.86s 12.36s p=0.147 n=6
Bind Time 2.64s (± 0.83%) 2.65s (± 0.21%) ~ 2.64s 2.65s p=0.866 n=6
Check Time 102.00s (± 0.78%) 101.98s (± 0.69%) ~ 100.80s 102.64s p=0.936 n=6
Emit Time 0.32s (± 0.00%) 0.32s (± 0.00%) ~ 0.32s 0.32s p=1.000 n=6
Total Time 116.93s (± 0.66%) 117.06s (± 0.56%) ~ 115.84s 117.62s p=0.575 n=6
self-build-src - node (v18.15.0, x64)
Memory used 2,406,587k (± 0.02%) 2,406,502k (± 0.01%) ~ 2,406,313k 2,406,706k p=0.810 n=6
Parse Time 5.08s (± 0.69%) 5.09s (± 0.86%) ~ 5.01s 5.13s p=0.936 n=6
Bind Time 1.88s (± 0.48%) 1.88s (± 0.52%) ~ 1.87s 1.90s p=0.931 n=6
Check Time 33.63s (± 0.33%) 33.74s (± 0.25%) ~ 33.64s 33.88s p=0.093 n=6
Emit Time 2.66s (± 1.55%) 2.71s (± 1.25%) ~ 2.67s 2.76s p=0.127 n=6
Total Time 43.26s (± 0.26%) 43.44s (± 0.09%) +0.17s (+ 0.40%) 43.38s 43.48s p=0.013 n=6
self-compiler - node (v18.15.0, x64)
Memory used 419,234k (± 0.01%) 419,191k (± 0.01%) ~ 419,143k 419,253k p=0.092 n=6
Parse Time 2.81s (± 0.57%) 2.84s (± 0.62%) +0.03s (+ 1.01%) 2.82s 2.87s p=0.009 n=6
Bind Time 1.07s (± 0.70%) 1.07s (± 0.48%) ~ 1.07s 1.08s p=0.784 n=6
Check Time 15.23s (± 0.43%) 15.24s (± 0.37%) ~ 15.15s 15.30s p=0.808 n=6
Emit Time 1.14s (± 1.47%) 1.15s (± 1.23%) ~ 1.13s 1.17s p=0.367 n=6
Total Time 20.25s (± 0.28%) 20.30s (± 0.33%) ~ 20.20s 20.38s p=0.261 n=6
vscode - node (v18.15.0, x64)
Memory used 2,849,904k (± 0.00%) 2,849,911k (± 0.00%) ~ 2,849,845k 2,849,971k p=0.810 n=6
Parse Time 10.77s (± 0.37%) 10.73s (± 0.22%) ~ 10.70s 10.76s p=0.107 n=6
Bind Time 3.44s (± 0.35%) 3.44s (± 0.56%) ~ 3.42s 3.47s p=0.797 n=6
Check Time 60.75s (± 0.18%) 60.75s (± 0.59%) ~ 60.37s 61.29s p=0.873 n=6
Emit Time 16.25s (± 0.46%) 16.22s (± 0.65%) ~ 16.07s 16.36s p=0.521 n=6
Total Time 91.20s (± 0.16%) 91.13s (± 0.42%) ~ 90.68s 91.81s p=0.521 n=6
webpack - node (v18.15.0, x64)
Memory used 397,092k (± 0.02%) 397,071k (± 0.01%) ~ 397,011k 397,131k p=1.000 n=6
Parse Time 3.13s (± 0.56%) 3.14s (± 0.66%) ~ 3.13s 3.18s p=0.459 n=6
Bind Time 1.39s (± 0.98%) 1.39s (± 0.59%) ~ 1.38s 1.40s p=0.933 n=6
Check Time 14.10s (± 0.23%) 14.07s (± 0.38%) ~ 14.03s 14.17s p=0.198 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 18.62s (± 0.27%) 18.61s (± 0.29%) ~ 18.56s 18.69s p=0.747 n=6
xstate - node (v18.15.0, x64)
Memory used 513,428k (± 0.01%) 513,444k (± 0.01%) ~ 513,382k 513,514k p=0.689 n=6
Parse Time 3.28s (± 0.27%) 3.28s (± 0.17%) ~ 3.27s 3.28s p=0.341 n=6
Bind Time 1.54s (± 0.26%) 1.54s (± 0.54%) ~ 1.54s 1.56s p=0.527 n=6
Check Time 2.87s (± 0.68%) 2.87s (± 0.72%) ~ 2.84s 2.89s p=1.000 n=6
Emit Time 0.08s (± 0.00%) 0.08s (± 0.00%) ~ 0.08s 0.08s p=1.000 n=6
Total Time 7.78s (± 0.16%) 7.78s (± 0.16%) ~ 7.76s 7.79s p=1.000 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the top-repos suite comparing main and refs/pull/57484/merge:

Everything looks good!

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM though I am surprised that no other baselines changed.

@jakebailey
Copy link
Member

To be clear, this doesn't actually fix #57483, though, right? As we don't have the follow-up to continue node reuse?

@dragomirtitian
Copy link
Contributor Author

To be clear, this doesn't actually fix #57483, though, right? As we don't have the follow-up to continue node reuse?

It reuses type nodes if we know they are correct (if they already contain undefined). This is better than the situation before where it never reused the declared type node (see test output before the change). A follow on PR can always reuse the type node and add | undefined even if the declared type already includes it.

@dragomirtitian
Copy link
Contributor Author

To be clear, this doesn't actually fix #57483, though, right? As we don't have the follow-up to continue node reuse?

It reuses type nodes if we know they are correct (if they already contain undefined). This is better than the situation before where it never reused the declared type node (see test output before the change). A follow on PR can always reuse the type node and add | undefined even if the declared type already includes it.

Upon further reflection, adding | undefined to all types will be a problem. People might raise bugs because their types are not the same as written as the | undefined will be redundant in some cases. It also goes against the general idea that in isolated declarations we keep the types you write.

I think this PR gets us as close to what an external tool could reasonably emit. The cases where the type is resolved in the new tests will be errors under --isolatedDeclarations

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's pretty clear. I think this PR is positive then, in that we're increasing node reuse (and under isolated mode, the cases that don't have reuse are just going to be illegal anyway).

@jakebailey jakebailey merged commit e089896 into microsoft:main Feb 29, 2024
19 checks passed
@dragomirtitian dragomirtitian changed the title Preserve parameter types for optional parameters /fields with undefined in type and for required params with default value [ID-Prep] PR3 - Preserve parameter types for optional parameters /fields with undefined in type and for required params with default value Mar 7, 2024
dragomirtitian added a commit to bloomberg/TypeScript that referenced this pull request Mar 11, 2024
…ed in type and for required params with default value (microsoft#57484)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
None yet
3 participants