fix54035, extractType now allows parts of union and intersection types to be extracted (#56131)

This commit is contained in:
Isabel Duan
2023-10-27 17:59:16 -07:00
committed by GitHub
parent f25f2bb75d
commit 6061069d96
13 changed files with 337 additions and 36 deletions

View File

@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
//// type A = { a: string } | /*1*/{ b: string } | { c: string }/*2*/ | { d: string };
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
b: string;
} | {
c: string;
};
type A = { a: string } | NewType | { d: string };`,
});

View File

@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
//// type B = string;
//// type C = number;
//// type A = { a: string } | /*1*/B | C/*2*/;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type B = string;
type C = number;
type /*RENAME*/NewType = B | C;
type A = { a: string } | NewType;`,
});

View File

@@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />
//// type B = string;
//// type C = number;
////
//// export function foo<T extends boolean | /*1*/B | C/*2*/>(x: T): T {
//// return x;
//// }
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type B = string;
type C = number;
type /*RENAME*/NewType = B | C;
export function foo<T extends boolean | NewType>(x: T): T {
return x;
}`,
});

View File

@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
//// type A = { a: string } & /*1*/{ b: string } & { c: string }/*2*/;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to interface",
actionDescription: "Extract to interface",
newContent:
`interface /*RENAME*/NewType {
b: string;
c: string;
}
type A = { a: string } & NewType;`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//// type A<T,S> = /*1*/{ a: string } | { b: T } | { c: string }/*2*/ | { d: string } | S;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType<T> = {
a: string;
} | {
b: T;
} | {
c: string;
};
type A<T,S> = NewType<T> | { d: string } | S;`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//// type A = { a: str/*1*/ing } | { b: string } | { c: string }/*2*/;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
a: string;
} | {
b: string;
} | {
c: string;
};
type A = NewType;`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//// type A = { a: string /*1*/} | { b: string } | { c: string }/*2*/;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
a: string;
} | {
b: string;
} | {
c: string;
};
type A = NewType;`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//// type A = { a: string } /*1*/| { b: string } | { c: string }/*2*/;
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
a: string;
} | {
b: string;
} | {
c: string;
};
type A = NewType;`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//// type A = {/*1*/ a: string } | { b: string } | { /*2*/c: string };
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
a: string;
} | {
b: string;
} | {
c: string;
};
type A = NewType;`,
});

View File

@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
//// type A = /*1*/{ a: string } | { b: string } |/*2*/ { c: string };
goTo.select("1", "2");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent:
`type /*RENAME*/NewType = {
a: string;
} | {
b: string;
};
type A = NewType | { c: string };`,
});

View File

@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />
//// type B = { b: string };
//// type C = { c: number };
////
//// interface X<T extends { prop: /*1*/T | /*3*/B /*2*/| C/*4*/ }> {}
goTo.select("1", "2");
verify.not.refactorAvailable("Extract type");
goTo.select("3", "4");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to type alias",
actionDescription: "Extract to type alias",
newContent: `type B = { b: string };
type C = { c: number };
type /*RENAME*/NewType = B | C;
interface X<T extends { prop: T | NewType }> {}`,
});

View File

@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
// @allowJs: true
// @Filename: a.js
//// /** @type { /*a*/string | number/*b*/ | boolean } */
//// var x;
goTo.file('a.js')
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Extract type",
actionName: "Extract to typedef",
actionDescription: "Extract to typedef",
newContent: `/**
* @typedef {string | number} /*RENAME*/NewType
*/
/** @type { NewType | boolean } */
var x;`,
});