Skip to content

Fixed JSX attributes discriminating based on optional children #53980

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

Merged

Conversation

Andarist
Copy link
Contributor

fixes #53941

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Apr 24, 2023
@@ -29336,14 +29336,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function discriminateContextualTypeByJSXAttributes(node: JsxAttributes, contextualType: UnionType) {
const jsxChildrenPropertyName = getJsxElementChildrenPropertyName(getJsxNamespaceAt(node));
return discriminateTypeByDiscriminableItems(contextualType,
concatenate(
map(
filter(node.properties, p => !!p.symbol && p.kind === SyntaxKind.JsxAttribute && isDiscriminantProperty(contextualType, p.symbol.escapedName) && (!p.initializer || isPossiblyDiscriminantValue(p.initializer))),
Copy link
Contributor Author

@Andarist Andarist Apr 24, 2023

Choose a reason for hiding this comment

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

I came to the conclusion that this "branch" doesn't need any immediate adjustments because JSX expressions can't be easily used as value-based discriminants.

  1. JsxText is always typed as stringType (no string literal types there)
  2. more than 1 children also can't be a discriminant value because arrays~ can't be such

In theory, it's actually possible to have things like:

type Props = {
  children: "a"
  foo: string
} | {
  children: "b"
  foo: number
}

;<Comp foo="bar">{"a"}</Comp>

So maybe this should be improved further, for this very specific use case - just to be consistent with:

;<Comp foo="bar" children={"a"} />

I think though that this can be done in a separate PR (if requested). It would be somewhat weird though that those 2 wouldn't behave the same, so perhaps further adjustments (beyond this function here) should be introduced in such a PR:

;<Comp foo="bar">{"a"}</Comp>
;<Comp foo="bar">a</Comp>

Note that this doesn't work in 5.0 anyway so likely I didn't regress this in #53502: TS playground 5.0

@jakebailey
Copy link
Member

@typescript-bot test this
@typescript-bot test top100
@typescript-bot user test this
@typescript-bot run dt
@typescript-bot perf test this faster
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

Heya @jakebailey, I've started to run the extended test suite on this PR at 862f863. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

Update: The results are in!

@typescript-bot
Copy link
Collaborator

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/153163/artifacts?artifactName=tgz&fileId=C70865149B9732B59DAE09EC4D27B6B49201F9264A4A00AA85C6A6C842F397AA02&fileName=/typescript-5.1.0-insiders.20230424.tgz"
    }
}

and then running npm install.

@typescript-bot
Copy link
Collaborator

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

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

Comparison Report - main..53980

Metric main 53980 Delta Best Worst p-value
Angular - node (v16.17.1, x64)
Memory used 365,207k (± 0.01%) 365,219k (± 0.00%) ~ 365,203k 365,236k p=0.378 n=6
Parse Time 3.54s (± 0.43%) 3.54s (± 0.50%) ~ 3.52s 3.57s p=0.570 n=6
Bind Time 1.18s (± 0.54%) 1.18s (± 0.35%) ~ 1.17s 1.18s p=0.673 n=6
Check Time 9.54s (± 0.65%) 9.54s (± 0.34%) ~ 9.50s 9.59s p=0.628 n=6
Emit Time 7.91s (± 0.29%) 7.91s (± 1.02%) ~ 7.84s 8.01s p=0.936 n=6
Total Time 22.17s (± 0.35%) 22.17s (± 0.35%) ~ 22.06s 22.28s p=0.873 n=6
Compiler-Unions - node (v16.17.1, x64)
Memory used 192,615k (± 0.04%) 192,632k (± 0.03%) ~ 192,559k 192,712k p=0.575 n=6
Parse Time 1.59s (± 0.47%) 1.59s (± 1.08%) ~ 1.56s 1.61s p=0.672 n=6
Bind Time 0.83s (± 1.01%) 0.83s (± 0.91%) ~ 0.82s 0.84s p=0.432 n=6
Check Time 10.22s (± 0.12%) 10.28s (± 0.32%) +0.06s (+ 0.60%) 10.24s 10.32s p=0.005 n=6
Emit Time 3.01s (± 0.39%) 3.01s (± 0.70%) ~ 2.98s 3.03s p=1.000 n=6
Total Time 15.64s (± 0.05%) 15.70s (± 0.22%) +0.06s (+ 0.37%) 15.66s 15.75s p=0.005 n=6
Monaco - node (v16.17.1, x64)
Memory used 345,780k (± 0.00%) 345,773k (± 0.00%) ~ 345,757k 345,786k p=0.296 n=6
Parse Time 2.71s (± 0.19%) 2.73s (± 0.60%) ~ 2.71s 2.75s p=0.143 n=6
Bind Time 1.08s (± 0.77%) 1.09s (± 1.50%) ~ 1.06s 1.11s p=0.612 n=6
Check Time 7.84s (± 0.10%) 7.89s (± 0.45%) +0.05s (+ 0.64%) 7.85s 7.95s p=0.007 n=6
Emit Time 4.45s (± 0.28%) 4.48s (± 0.84%) ~ 4.43s 4.53s p=0.146 n=6
Total Time 16.09s (± 0.17%) 16.18s (± 0.40%) +0.09s (+ 0.59%) 16.12s 16.29s p=0.013 n=6
TFS - node (v16.17.1, x64)
Memory used 300,081k (± 0.00%) 300,071k (± 0.01%) ~ 300,046k 300,098k p=0.298 n=6
Parse Time 2.15s (± 0.46%) 2.16s (± 0.49%) ~ 2.14s 2.17s p=0.673 n=6
Bind Time 1.24s (± 0.41%) 1.23s (± 0.66%) -0.02s (- 1.34%) 1.22s 1.24s p=0.009 n=6
Check Time 7.24s (± 0.38%) 7.24s (± 0.61%) ~ 7.19s 7.31s p=1.000 n=6
Emit Time 4.40s (± 1.06%) 4.37s (± 0.53%) ~ 4.34s 4.40s p=0.376 n=6
Total Time 15.03s (± 0.49%) 14.99s (± 0.48%) ~ 14.90s 15.09s p=0.520 n=6
material-ui - node (v16.17.1, x64)
Memory used 481,566k (± 0.01%) 481,619k (± 0.00%) +54k (+ 0.01%) 481,605k 481,638k p=0.045 n=6
Parse Time 3.23s (± 0.13%) 3.25s (± 0.41%) +0.02s (+ 0.52%) 3.23s 3.26s p=0.024 n=6
Bind Time 0.94s (± 0.80%) 0.94s (± 0.67%) ~ 0.93s 0.95s p=0.718 n=6
Check Time 17.90s (± 0.65%) 17.88s (± 0.60%) ~ 17.79s 18.09s p=0.630 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.07s (± 0.52%) 22.07s (± 0.44%) ~ 22.00s 22.26s p=0.936 n=6
xstate - node (v16.17.1, x64)
Memory used 560,542k (± 0.02%) 560,613k (± 0.02%) ~ 560,477k 560,734k p=0.199 n=6
Parse Time 4.00s (± 0.34%) 4.01s (± 0.32%) ~ 4.00s 4.03s p=0.096 n=6
Bind Time 1.77s (± 0.23%) 1.77s (± 0.23%) ~ 1.77s 1.78s p=0.218 n=6
Check Time 3.04s (± 0.34%) 3.05s (± 0.57%) ~ 3.04s 3.08s p=0.558 n=6
Emit Time 0.09s (± 0.00%) 0.09s (± 0.00%) ~ 0.09s 0.09s p=1.000 n=6
Total Time 8.90s (± 0.26%) 8.93s (± 0.37%) ~ 8.89s 8.97s p=0.224 n=6
System
Machine Namets-ci-ubuntu
Platformlinux 5.4.0-135-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v16.17.1, x64)
Scenarios
  • Angular - node (v16.17.1, x64)
  • Compiler-Unions - node (v16.17.1, x64)
  • Monaco - node (v16.17.1, x64)
  • TFS - node (v16.17.1, x64)
  • material-ui - node (v16.17.1, x64)
  • xstate - node (v16.17.1, x64)
Benchmark Name Iterations
Current 53980 6
Baseline main 6

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

Hey @jakebailey, the results of running the DT tests are ready.
There were interesting changes:

Main only errors:

Package: wordpress__components
Error:

Error: /home/vsts/work/1/s/DefinitelyTyped-tools/packages/dtslint-runner/DefinitelyTyped/types/wordpress__components/wordpress__components-tests.tsx:291:9
ERROR: 291:9  expect  TypeScript@local compile error: 
Binding element 'onClose' implicitly has an 'any' type.

    at testTypesVersion (/home/vsts/work/1/s/DefinitelyTyped-tools/packages/dtslint-runner/node_modules/@definitelytyped/dtslint/dist/index.js:194:15)
    at async runTests (/home/vsts/work/1/s/DefinitelyTyped-tools/packages/dtslint-runner/node_modules/@definitelytyped/dtslint/dist/index.js:151:9)

You can check the log here.

@jakebailey
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 24, 2023

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/153178/artifacts?artifactName=tgz&fileId=C70865149B9732B59DAE09EC4D27B6B49201F9264A4A00AA85C6A6C842F397AA02&fileName=/typescript-5.1.0-insiders.20230424.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.1.0-pr-53980-14".;

@weswigham weswigham merged commit 2cbfb51 into microsoft:main Apr 26, 2023
@Andarist Andarist deleted the fix/jsx-children-as-optional-discriminant branch April 26, 2023 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#53502 breaks @wordpress/components types on DT
4 participants