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

Treat contextually typed functions in JS files as typed #56907

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #56900

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 30, 2023
@jakebailey
Copy link
Member

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

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 2024

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 2024

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 2024

Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at 9a0c1d6. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 2024

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 2024

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 4, 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/159255/artifacts?artifactName=tgz&fileId=EB4F0807D220309B355F9F343E9CE2CD5748423A179B21BBD0DE75F81484C1CE02&fileName=/typescript-5.4.0-insiders.20240104.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.4.0-pr-56907-6".;

@typescript-bot
Copy link
Collaborator

@jakebailey Here are the results of running the user test suite comparing main and refs/pull/56907/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

Hey @jakebailey, the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@typescript-bot
Copy link
Collaborator

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

Here they are:

Compiler

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,446k (± 0.01%) 295,466k (± 0.01%) ~ 295,417k 295,518k p=0.521 n=6
Parse Time 2.65s (± 0.21%) 2.65s (± 0.40%) ~ 2.63s 2.66s p=1.000 n=6
Bind Time 0.82s (± 1.00%) 0.82s (± 0.77%) ~ 0.81s 0.83s p=0.599 n=6
Check Time 8.17s (± 0.49%) 8.15s (± 0.25%) ~ 8.13s 8.18s p=0.333 n=6
Emit Time 7.10s (± 0.31%) 7.10s (± 0.15%) ~ 7.08s 7.11s p=0.623 n=6
Total Time 18.73s (± 0.31%) 18.71s (± 0.14%) ~ 18.68s 18.75s p=0.420 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 192,467k (± 1.22%) 194,416k (± 1.65%) ~ 191,469k 197,413k p=0.471 n=6
Parse Time 1.35s (± 1.85%) 1.36s (± 1.46%) ~ 1.32s 1.38s p=0.871 n=6
Bind Time 0.72s (± 0.00%) 0.72s (± 0.00%) ~ 0.72s 0.72s p=1.000 n=6
Check Time 9.27s (± 0.44%) 9.24s (± 0.31%) ~ 9.19s 9.27s p=0.260 n=6
Emit Time 2.62s (± 1.09%) 2.62s (± 1.01%) ~ 2.59s 2.66s p=0.807 n=6
Total Time 13.97s (± 0.28%) 13.94s (± 0.19%) ~ 13.91s 13.97s p=0.225 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,410k (± 0.01%) 347,402k (± 0.00%) ~ 347,382k 347,427k p=0.575 n=6
Parse Time 2.47s (± 0.43%) 2.46s (± 0.60%) ~ 2.43s 2.47s p=0.554 n=6
Bind Time 0.93s (± 0.59%) 0.93s (± 0.44%) ~ 0.92s 0.93s p=0.282 n=6
Check Time 6.87s (± 0.25%) 6.87s (± 0.61%) ~ 6.84s 6.95s p=0.570 n=6
Emit Time 4.06s (± 0.29%) 4.05s (± 0.19%) ~ 4.04s 4.06s p=0.054 n=6
Total Time 14.33s (± 0.19%) 14.30s (± 0.24%) ~ 14.27s 14.36s p=0.260 n=6
TFS - node (v18.15.0, x64)
Memory used 302,725k (± 0.01%) 302,736k (± 0.01%) ~ 302,711k 302,759k p=0.748 n=6
Parse Time 1.99s (± 0.55%) 2.01s (± 1.50%) ~ 1.96s 2.05s p=0.250 n=6
Bind Time 1.01s (± 0.97%) 1.00s (± 1.17%) ~ 0.99s 1.02s p=0.066 n=6
Check Time 6.28s (± 0.33%) 6.30s (± 0.23%) ~ 6.28s 6.32s p=0.256 n=6
Emit Time 3.58s (± 0.48%) 3.58s (± 0.29%) ~ 3.57s 3.60s p=0.806 n=6
Total Time 12.87s (± 0.22%) 12.89s (± 0.28%) ~ 12.83s 12.93s p=0.195 n=6
material-ui - node (v18.15.0, x64)
Memory used 506,810k (± 0.00%) 506,831k (± 0.01%) ~ 506,773k 506,870k p=0.149 n=6
Parse Time 2.58s (± 0.76%) 2.58s (± 0.41%) ~ 2.57s 2.60s p=0.802 n=6
Bind Time 0.98s (± 1.40%) 0.99s (± 0.52%) ~ 0.99s 1.00s p=0.111 n=6
Check Time 16.97s (± 0.44%) 16.96s (± 0.39%) ~ 16.88s 17.07s p=0.873 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 20.54s (± 0.31%) 20.54s (± 0.32%) ~ 20.47s 20.65s p=1.000 n=6
xstate - node (v18.15.0, x64)
Memory used 512,892k (± 0.01%) 512,824k (± 0.01%) ~ 512,722k 512,925k p=0.128 n=6
Parse Time 3.27s (± 0.32%) 3.27s (± 0.23%) ~ 3.26s 3.28s p=0.611 n=6
Bind Time 1.54s (± 0.36%) 1.54s (± 0.36%) ~ 1.53s 1.54s p=1.000 n=6
Check Time 2.84s (± 0.96%) 2.83s (± 0.37%) ~ 2.81s 2.84s p=0.809 n=6
Emit Time 0.07s (± 0.00%) 0.07s (± 0.00%) ~ 0.07s 0.07s p=1.000 n=6
Total Time 7.72s (± 0.32%) 7.72s (± 0.25%) ~ 7.69s 7.74s p=0.872 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)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

tsserver

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-UnionsTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,342ms (± 0.97%) 2,350ms (± 0.91%) ~ 2,316ms 2,375ms p=0.471 n=6
Req 2 - geterr 5,448ms (± 1.94%) 5,447ms (± 1.38%) ~ 5,364ms 5,524ms p=1.000 n=6
Req 3 - references 328ms (± 1.58%) 325ms (± 1.17%) ~ 320ms 331ms p=0.295 n=6
Req 4 - navto 274ms (± 1.10%) 275ms (± 1.05%) ~ 272ms 279ms p=0.935 n=6
Req 5 - completionInfo count 1,356 (± 0.00%) 1,356 (± 0.00%) ~ 1,356 1,356 p=1.000 n=6
Req 5 - completionInfo 92ms (± 4.92%) 92ms (± 4.50%) ~ 84ms 95ms p=1.000 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,500ms (± 0.83%) 2,474ms (± 1.08%) ~ 2,441ms 2,514ms p=0.092 n=6
Req 2 - geterr 4,139ms (± 1.80%) 4,159ms (± 2.01%) ~ 4,078ms 4,238ms p=1.000 n=6
Req 3 - references 338ms (± 0.98%) 335ms (± 1.39%) ~ 332ms 344ms p=0.167 n=6
Req 4 - navto 285ms (± 1.03%) 286ms (± 1.31%) ~ 283ms 291ms p=0.931 n=6
Req 5 - completionInfo count 1,518 (± 0.00%) 1,518 (± 0.00%) ~ 1,518 1,518 p=1.000 n=6
Req 5 - completionInfo 85ms (± 7.16%) 83ms (± 6.19%) ~ 78ms 89ms p=0.415 n=6
xstateTSServer - node (v18.15.0, x64)
Req 1 - updateOpen 2,609ms (± 0.63%) 2,595ms (± 0.83%) ~ 2,575ms 2,620ms p=0.470 n=6
Req 2 - geterr 1,738ms (± 2.36%) 1,693ms (± 2.79%) ~ 1,639ms 1,764ms p=0.128 n=6
Req 3 - references 108ms (± 9.55%) 113ms (±10.71%) ~ 101ms 125ms p=0.285 n=6
Req 4 - navto 362ms (± 1.14%) 365ms (± 0.24%) ~ 364ms 366ms p=0.084 n=6
Req 5 - completionInfo count 2,073 (± 0.00%) 2,073 (± 0.00%) ~ 2,073 2,073 p=1.000 n=6
Req 5 - completionInfo 310ms (± 1.87%) 303ms (± 2.16%) ~ 296ms 312ms p=0.128 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • CompilerTSServer - node (v18.15.0, x64)
  • Compiler-UnionsTSServer - node (v18.15.0, x64)
  • xstateTSServer - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Startup

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
tsc-startup - node (v18.15.0, x64)
Execution time 153.75ms (± 0.20%) 153.65ms (± 0.19%) -0.10ms (- 0.06%) 152.60ms 156.13ms p=0.002 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time 228.47ms (± 0.15%) 228.33ms (± 0.17%) -0.14ms (- 0.06%) 227.09ms 233.34ms p=0.000 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time 230.75ms (± 0.21%) 230.53ms (± 0.18%) -0.22ms (- 0.09%) 228.62ms 234.69ms p=0.000 n=600
typescript-startup - node (v18.15.0, x64)
Execution time 230.35ms (± 0.21%) 230.32ms (± 0.19%) ~ 228.36ms 235.21ms p=0.845 n=600
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • tsc-startup - node (v18.15.0, x64)
  • tsserver-startup - node (v18.15.0, x64)
  • tsserverlibrary-startup - node (v18.15.0, x64)
  • typescript-startup - 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/56907/merge:

Everything looks good!

@sandersn sandersn added this to Not started in PR Backlog Jan 23, 2024
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

I think looking for a contextual signature might be incorrect unless there's somethiing inherently contextual about @satisfies that @type doesn't have.

@@ -15107,7 +15107,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
isInJSFile(declaration) &&
isValueSignatureDeclaration(declaration) &&
!hasJSDocParameterTags(declaration) &&
!getJSDocType(declaration);
!getJSDocType(declaration) &&
!getContextualSignatureForFunctionLikeDeclaration(declaration);
Copy link
Member

Choose a reason for hiding this comment

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

Does this change the semantics of @type, or is it specific to @satisfies? For example, in

/** @type {(uuid: string) => string}
const f = uuid => uuid

I recall that @type intentionally is not treated as a contextual signature in that case, but I could be mistaken. I believe that instead the @type tag is looked up and treated the same as if it were written:

const f = /** @type {(uuid: string) => }*/(uuid => uuid)

That is, treated like a cast. Probably @satisfies should be the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It changes the semantics of the second example but not the first one. @type above the declaration is treated as the contextual signature (getContextualSignature calls getSignatureOfTypeTag and returns that if it's found).

That said, I'm not sure if this is a bad change. This flag has one purpose - it changes how getMinArgumentCount gets computed. If the types of parameters are assigned in just any way, I think it's surprising that the resulting signature might get treated as "untyped" and that it might allow calls without all (seemingly) required parameters. Just take a look at the situation reported in this thread and at the discussion happening there. While this PR here doesn't solve the problem described there with @type applied to parameters, the root cause is similar - despite the call being typed it's treated as untyped. This creates weird discrepancies between TS and JS files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An extra data point:

const f1 = /** @type {(uuid: string) => {}}*/(uuid => uuid)
f1() // error

const f2 = /** @satisfies {(uuid: string) => {}}*/(uuid => uuid)
f2() // ok, wat?

The reason behind the difference here is that with the cast the inner signature is untyped but the final type of f1 is sourced from the casted type itself. Since that's a type node~ it can't be untyped.

To give more context on the above:

  • checkAssertionWorker computes exprType with an untyped signature (but its parameters are typed!)
  • but it returns getTypeFromTypeNode(type)

This can't work like that with @satisfies as the @satifies merely provides some contextual typing - it doesn't "override" the expression type.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, that explanation helps. I was getting side-tracked by having the jsdoc on the declaration instead of directly on the arrow.

PR Backlog automation moved this from Not started to Waiting on author Jan 29, 2024
@sandersn sandersn self-assigned this Jan 29, 2024
@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 Jan 29, 2024
@Andarist Andarist requested a review from sandersn May 21, 2024 09:05
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

This makes sense to me as a place that should have been contextually typed before, but wasn't.

@@ -15107,7 +15107,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
isInJSFile(declaration) &&
isValueSignatureDeclaration(declaration) &&
!hasJSDocParameterTags(declaration) &&
!getJSDocType(declaration);
!getJSDocType(declaration) &&
!getContextualSignatureForFunctionLikeDeclaration(declaration);
Copy link
Member

Choose a reason for hiding this comment

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

Thanks, that explanation helps. I was getting side-tracked by having the jsdoc on the declaration instead of directly on the arrow.

PR Backlog automation moved this from Waiting on author to Needs merge May 21, 2024
@sandersn
Copy link
Member

@Andarist can you merge this from main and then I'll run typescript-bot test it before merging it.

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
Status: Needs merge
PR Backlog
  
Needs merge
Development

Successfully merging this pull request may close these issues.

Using @satisfies tag in JSDoc has strange effects on arrow function parameters
4 participants