feat: support variance annotations on type parameters (TypeScript 4.7)#361
Draft
marionebl wants to merge 1 commit intotree-sitter:masterfrom
Draft
feat: support variance annotations on type parameters (TypeScript 4.7)#361marionebl wants to merge 1 commit intotree-sitter:masterfrom
marionebl wants to merge 1 commit intotree-sitter:masterfrom
Conversation
TypeScript 4.7 introduced `in`/`out` variance annotations on type
parameters to explicitly declare covariance and contravariance:
interface Covariant<out T> {} // out → covariant
interface Contravariant<in T> {} // in → contravariant
interface Invariant<in out T> {} // in out → invariant
Add a new named `variance` node and make it optional at the start of
`type_parameter`, consistent with the existing optional `const`
modifier for const type parameters (TypeScript 5.0).
Variance can be combined with constraints and defaults:
interface Queue<out A, out R = never> {}
type Covariant<out T extends string> = T;
Add two corpus test cases in `test/corpus/types.txt`.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
TypeScript 4.7 introduced
in/outvariance annotations on generic type parameters to explicitly declare variance:These were previously parsed as ERROR nodes. In effect-ts for example, 316 out of 1 767
.tsfiles contain variance annotations, producing 3 160 parse errors.Changes
common/define-grammar.jsvariancerule (modelled afteroverride_modifier):type_parametergainsoptional($.variance)between the existingoptional('const')andfield('name', …).Using a named node (rather than inline anonymous tokens) makes the annotation queryable via tree-sitter node queries.
test/corpus/types.txt— two new corpus test cases:Variance annotations— all three forms + multiple params in a type aliasVariance annotations with constraints and defaults— real-world patterns from effect-tsTest results
Reference