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

Support TS 4.7 optional variance #12900

Merged
merged 3 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/language-js/print/type-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ function printTypeParameter(path, options, print) {
parts.push(print("variance"));
}

if (node.in) {
parts.push("in ");
}

if (node.out) {
parts.push("out ");
}

parts.push(print("name"));

if (node.bound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,280 @@ new A<T>();
================================================================================
`;

exports[`ts-4.7-optional-variance.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
printWidth: 80
| printWidth
=====================================input======================================
type Covariant<out T> = {
x: T;
}
type Contravariant<in T> = {
f: (x: T) => void;
}
type Invariant<in out T> = {
f: (x: T) => T;
}
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];

type Covariant1<in T> = {
x: T;
}

type Contravariant1<out T> = keyof T;

type Contravariant2<out T> = {
f: (x: T) => void;
}

type Invariant1<in T> = {
f: (x: T) => T;
}

type Invariant2<out T> = {
f: (x: T) => T;
}
type Foo1<in T> = {
x: T;
f: FooFn1<T>;
}

type Foo2<out T> = {
x: T;
f: FooFn2<T>;
}

type Foo3<in out T> = {
x: T;
f: FooFn3<T>;
}

type T21<in out T> = T;

interface Baz<out T> {}
interface Baz<in T> {}

interface Parent<out A> {
child: Child<A> | null;
parent: Parent<A> | null;
}

declare class StateNode<TContext, in out TEvent extends { type: string }> {
_storedEvent: TEvent;
_action: ActionObject<TEvent>;
_state: StateNode<TContext, any>;
}

=====================================output=====================================
type Covariant<out T> = {
x: T;
};
type Contravariant<in T> = {
f: (x: T) => void;
};
type Invariant<in out T> = {
f: (x: T) => T;
};
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];

type Covariant1<in T> = {
x: T;
};

type Contravariant1<out T> = keyof T;

type Contravariant2<out T> = {
f: (x: T) => void;
};

type Invariant1<in T> = {
f: (x: T) => T;
};

type Invariant2<out T> = {
f: (x: T) => T;
};
type Foo1<in T> = {
x: T;
f: FooFn1<T>;
};

type Foo2<out T> = {
x: T;
f: FooFn2<T>;
};

type Foo3<in out T> = {
x: T;
f: FooFn3<T>;
};

type T21<in out T> = T;

interface Baz<out T> {}
interface Baz<in T> {}

interface Parent<out A> {
child: Child<A> | null;
parent: Parent<A> | null;
}

declare class StateNode<TContext, in out TEvent extends { type: string }> {
_storedEvent: TEvent;
_action: ActionObject<TEvent>;
_state: StateNode<TContext, any>;
}

================================================================================
`;

exports[`ts-4.7-optional-variance-with-jsx.tsx format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
printWidth: 80
| printWidth
=====================================input======================================
// valid JSX
<in T>() => {}</in>;

type Covariant<out T> = {
x: T;
}
type Contravariant<in T> = {
f: (x: T) => void;
}
type Invariant<in out T> = {
f: (x: T) => T;
}
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];

type Covariant1<in T> = {
x: T;
}

type Contravariant1<out T> = keyof T;

type Contravariant2<out T> = {
f: (x: T) => void;
}

type Invariant1<in T> = {
f: (x: T) => T;
}

type Invariant2<out T> = {
f: (x: T) => T;
}
type Foo1<in T> = {
x: T;
f: FooFn1<T>;
}

type Foo2<out T> = {
x: T;
f: FooFn2<T>;
}

type Foo3<in out T> = {
x: T;
f: FooFn3<T>;
}

type T21<in out T> = T;

interface Baz<out T> {}
interface Baz<in T> {}

interface Parent<out A> {
child: Child<A> | null;
parent: Parent<A> | null;
}

declare class StateNode<TContext, in out TEvent extends { type: string }> {
_storedEvent: TEvent;
_action: ActionObject<TEvent>;
_state: StateNode<TContext, any>;
}

=====================================output=====================================
// valid JSX
<in T>() => {}</in>;

type Covariant<out T> = {
x: T;
};
type Contravariant<in T> = {
f: (x: T) => void;
};
type Invariant<in out T> = {
f: (x: T) => T;
};
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];

type Covariant1<in T> = {
x: T;
};

type Contravariant1<out T> = keyof T;

type Contravariant2<out T> = {
f: (x: T) => void;
};

type Invariant1<in T> = {
f: (x: T) => T;
};

type Invariant2<out T> = {
f: (x: T) => T;
};
type Foo1<in T> = {
x: T;
f: FooFn1<T>;
};

type Foo2<out T> = {
x: T;
f: FooFn2<T>;
};

type Foo3<in out T> = {
x: T;
f: FooFn3<T>;
};

type T21<in out T> = T;

interface Baz<out T> {}
interface Baz<in T> {}

interface Parent<out A> {
child: Child<A> | null;
parent: Parent<A> | null;
}

declare class StateNode<TContext, in out TEvent extends { type: string }> {
_storedEvent: TEvent;
_action: ActionObject<TEvent>;
_state: StateNode<TContext, any>;
}

================================================================================
`;

exports[`tuple-labeled-ts.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// valid JSX
<in T>() => {}</in>;

type Covariant<out T> = {
x: T;
}
type Contravariant<in T> = {
f: (x: T) => void;
}
type Invariant<in out T> = {
f: (x: T) => T;
}
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];

type Covariant1<in T> = {
x: T;
}

type Contravariant1<out T> = keyof T;

type Contravariant2<out T> = {
f: (x: T) => void;
}

type Invariant1<in T> = {
f: (x: T) => T;
}

type Invariant2<out T> = {
f: (x: T) => T;
}
type Foo1<in T> = {
x: T;
f: FooFn1<T>;
}

type Foo2<out T> = {
x: T;
f: FooFn2<T>;
}

type Foo3<in out T> = {
x: T;
f: FooFn3<T>;
}

type T21<in out T> = T;

interface Baz<out T> {}
interface Baz<in T> {}

interface Parent<out A> {
child: Child<A> | null;
parent: Parent<A> | null;
}

declare class StateNode<TContext, in out TEvent extends { type: string }> {
_storedEvent: TEvent;
_action: ActionObject<TEvent>;
_state: StateNode<TContext, any>;
}