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
[ts 4.7] Support optional variance annotations #14359
Merged
nicolo-ribaudo
merged 9 commits into
babel:main
from
magic-akari:feat/ts-VarianceAnnotations
May 17, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ddc567d
feat: support optional variance annotations
magic-akari f30cbf0
fix: flow check
magic-akari c6db874
fix: BABEL_8_BREAKING
magic-akari c13020b
chore: add test cases
magic-akari a626604
fix: TS1274
magic-akari e89330a
fix: more recoverable error
magic-akari a19b618
Merge branch 'main' into feat/ts-VarianceAnnotations
magic-akari 9aa70e2
fix: flowjs type
magic-akari 13cd945
chore: update babel 8 test case
magic-akari File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/babel-generator/test/fixtures/typescript/variance-annotations-with-jsx/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// valid JSX | ||
<in T>() => {}</in>; | ||
|
||
type Covariant<out T> = { | ||
x: T; | ||
} | ||
|
||
declare let super_covariant: Covariant<unknown>; | ||
declare let sub_covariant: Covariant<string>; | ||
|
||
super_covariant = sub_covariant; | ||
sub_covariant = super_covariant; // Error | ||
|
||
type Contravariant<in T> = { | ||
f: (x: T) => void; | ||
} | ||
|
||
declare let super_contravariant: Contravariant<unknown>; | ||
declare let sub_contravariant: Contravariant<string>; | ||
|
||
super_contravariant = sub_contravariant; // Error | ||
sub_contravariant = super_contravariant; | ||
|
||
type Invariant<in out T> = { | ||
f: (x: T) => T; | ||
} | ||
|
||
declare let super_invariant: Invariant<unknown>; | ||
declare let sub_invariant: Invariant<string>; | ||
|
||
super_invariant = sub_invariant; // Error | ||
sub_invariant = super_invariant; // Error | ||
|
||
// Variance of various type constructors | ||
|
||
type T10<out T> = T; | ||
type T11<in T> = keyof T; | ||
type T12<out T, out K extends keyof T> = T[K]; | ||
type T13<in out T> = T[keyof T]; | ||
|
||
// Variance annotation errors | ||
|
||
type Covariant1<in T> = { // Error | ||
x: T; | ||
} | ||
|
||
type Contravariant1<out T> = keyof T; // Error | ||
|
||
type Contravariant2<out T> = { // Error | ||
f: (x: T) => void; | ||
} | ||
|
||
type Invariant1<in T> = { // Error | ||
f: (x: T) => T; | ||
} | ||
|
||
type Invariant2<out T> = { // Error | ||
f: (x: T) => T; | ||
} | ||
|
||
// Variance in circular types | ||
|
||
type Foo1<in T> = { // Error | ||
x: T; | ||
f: FooFn1<T>; | ||
} | ||
|
||
type FooFn1<T> = (foo: Bar1<T[]>) => void; | ||
|
||
type Bar1<T> = { | ||
value: Foo1<T[]>; | ||
} | ||
|
||
type Foo2<out T> = { // Error | ||
x: T; | ||
f: FooFn2<T>; | ||
} | ||
|
||
type FooFn2<T> = (foo: Bar2<T[]>) => void; | ||
|
||
type Bar2<T> = { | ||
value: Foo2<T[]>; | ||
} | ||
|
||
type Foo3<in out T> = { | ||
x: T; | ||
f: FooFn3<T>; | ||
} | ||
|
||
type FooFn3<T> = (foo: Bar3<T[]>) => void; | ||
|
||
type Bar3<T> = { | ||
value: Foo3<T[]>; | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-generator/test/fixtures/typescript/variance-annotations-with-jsx/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": ["jsx", "typescript"] | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/babel-generator/test/fixtures/typescript/variance-annotations-with-jsx/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// valid JSX | ||
<in T>() => {}</in>; | ||
type Covariant<out T> = { | ||
x: T; | ||
}; | ||
declare let super_covariant: Covariant<unknown>; | ||
declare let sub_covariant: Covariant<string>; | ||
super_covariant = sub_covariant; | ||
sub_covariant = super_covariant; // Error | ||
|
||
type Contravariant<in T> = { | ||
f: (x: T) => void; | ||
}; | ||
declare let super_contravariant: Contravariant<unknown>; | ||
declare let sub_contravariant: Contravariant<string>; | ||
super_contravariant = sub_contravariant; // Error | ||
|
||
sub_contravariant = super_contravariant; | ||
type Invariant<in out T> = { | ||
f: (x: T) => T; | ||
}; | ||
declare let super_invariant: Invariant<unknown>; | ||
declare let sub_invariant: Invariant<string>; | ||
super_invariant = sub_invariant; // Error | ||
|
||
sub_invariant = super_invariant; // Error | ||
// Variance of various type constructors | ||
|
||
type T10<out T> = T; | ||
type T11<in T> = keyof T; | ||
type T12<out T, out K extends keyof T> = T[K]; | ||
type T13<in out T> = T[keyof T]; // Variance annotation errors | ||
|
||
type Covariant1<in T> = { | ||
// Error | ||
x: T; | ||
}; | ||
type Contravariant1<out T> = keyof T; // Error | ||
|
||
type Contravariant2<out T> = { | ||
// Error | ||
f: (x: T) => void; | ||
}; | ||
type Invariant1<in T> = { | ||
// Error | ||
f: (x: T) => T; | ||
}; | ||
type Invariant2<out T> = { | ||
// Error | ||
f: (x: T) => T; | ||
}; // Variance in circular types | ||
|
||
type Foo1<in T> = { | ||
// Error | ||
x: T; | ||
f: FooFn1<T>; | ||
}; | ||
type FooFn1<T> = (foo: Bar1<T[]>) => void; | ||
type Bar1<T> = { | ||
value: Foo1<T[]>; | ||
}; | ||
type Foo2<out T> = { | ||
// Error | ||
x: T; | ||
f: FooFn2<T>; | ||
}; | ||
type FooFn2<T> = (foo: Bar2<T[]>) => void; | ||
type Bar2<T> = { | ||
value: Foo2<T[]>; | ||
}; | ||
type Foo3<in out T> = { | ||
x: T; | ||
f: FooFn3<T>; | ||
}; | ||
type FooFn3<T> = (foo: Bar3<T[]>) => void; | ||
type Bar3<T> = { | ||
value: Foo3<T[]>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a generator test case to
packages/babel-generator/test/fixtures/typescript
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm investigating ci failures in main branch, but I don't know much about ts and jsx.
<in T>() => {}</in>
will fail when BABEL_8_BREAKING is enabled.Can someone tell me if this is a problem with the parser or with the test?