Skip to content

[ts 4.7] Support extends constraints for infer#14476

Merged
nicolo-ribaudo merged 10 commits intobabel:mainfrom
sosukesuzuki:infer-extends
May 17, 2022
Merged

[ts 4.7] Support extends constraints for infer#14476
nicolo-ribaudo merged 10 commits intobabel:mainfrom
sosukesuzuki:infer-extends

Conversation

@sosukesuzuki
Copy link
Copy Markdown
Contributor

@sosukesuzuki sosukesuzuki commented Apr 20, 2022

Q                       A
Fixed Issues? N/A
Minor: New Feature? Y
Tests Added + Pass? Y
License MIT

Support https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#extends-constraints-on-infer-type-variables

type FirstString<T> =
    T extends [infer S extends string, ...unknown[]]
        ? S
        : never;

@sosukesuzuki sosukesuzuki added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: parser area: typescript labels Apr 20, 2022
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Apr 20, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51953/

this.expectContextual(tt._infer);
const typeParameter = this.startNode();
typeParameter.name = this.tsParseTypeParameterName();
typeParameter.constraint = this.tsEatThenParseType(tt._extends);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add this to the type definitions in https://github.com/babel/babel/blob/main/packages/babel-parser/src/types.js, in @babel/types and in @babel/generator?

Copy link
Copy Markdown
Contributor Author

@sosukesuzuki sosukesuzuki May 1, 2022

Choose a reason for hiding this comment

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

  • I've fixed type definition in @babel/parser a837b29
  • I've added tests in @babel/generator. bfce230
  • The type for TSInferType that is defined in @babel/types supports constraint field already. So I think we don't need to change @babel/types in this PR.

@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this behavior different in Babel 8? If it is, can you also add a Babel 8 test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In babel 8, the AST for type parameters will be changed. I'll add tests for babel 8 later!

Copy link
Copy Markdown
Contributor Author

@sosukesuzuki sosukesuzuki Apr 22, 2022

Choose a reason for hiding this comment

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

7c7d753 👍

@nicolo-ribaudo nicolo-ribaudo added this to the v7.18.0 milestone Apr 20, 2022
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung 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 we need to handle the precedence of extends vs ?. Can you add a new test case

type X<U, T> = T extends [infer U extends T ? U : T] ? U : T;

It is valid in tsc 4.7.0.

@sosukesuzuki
Copy link
Copy Markdown
Contributor Author

@JLHwung I've tried to support precedence of infer extends and conditional types ee9c29f.

Do you have a better solution?

@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Apr 22, 2022

Can you add a new test case?

type X<U, T> = [(infer U extends T)?];

is invalid according to tsc. Currently Babel 7 was passing while Babel 8 is throwing.

In microsoft/TypeScript#48112, tsc introduced a new parsing context DisallowConditionalTypesContext and apply it to extends type. Since TS doesn't have language spec anyway, I think we can follow tsc's approach and add a new production parameter accordingly.

@sosukesuzuki
Copy link
Copy Markdown
Contributor Author

@JLHwung Thank you for your review. I've refactored according to tsc implementation. c9a8e60

is invalid according to tsc.

It is semantically invalid, but seems to parse successfully(TS Playground link). Should Babel throw a recoverable error for it? If Babel needs to throw a recoverable error for it, I think the fix should be in a separate PR. This is because even with infer without extends, tsc throws a similar semantic error(TS Playground link), and Babel does not throw an error as well(Babel repl link).

"type": "TSTypeParameter",
"start":30,"end":46,"loc":{"start":{"line":1,"column":30,"index":30},"end":{"line":1,"column":46,"index":46}},
"name": "U",
"constraint": {
Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo May 3, 2022

Choose a reason for hiding this comment

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

I am really surprised by this placement. I'd expect constraint to be on TSInferType, not on TSTypeParameter. The new syntax is infer T extends U, not just T extends U.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think instead of adding a new constraint field to TSInferType, we should add a constraint to the typeParamter of TSInferType. Because it is the type parameter, not the infer, that is constrained.

FYI, the AST in TypeScript Compiler adds constaint to TypeParameter. (TypeScript AST Viewer Link)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tbh, I really dislike many choices that the TS team made with their AST 😛

However, I realized that your AST design follows the shape we already have for type X<A extends B> so it's fine.

@@ -0,0 +1,5 @@
type X3<T> = T extends [infer U extends number] ? MustBeNumber<U> : never;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add some tests without tuples?

type X<U, T> = T extends infer U extends number ? U : T;
type X<U, T> = T extends (infer U extends number ? U : T) ? U : T;

@nicolo-ribaudo nicolo-ribaudo changed the title feat(babel-parser): support TS 4.7 extends constraints for infer [ts 4.7] Support extends constraints for infer May 17, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit 59d24a4 into babel:main May 17, 2022
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants