mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
fix54035, extractType now allows parts of union and intersection types to be extracted (#56131)
This commit is contained in:
18
tests/cases/fourslash/refactorExtractType78.ts
Normal file
18
tests/cases/fourslash/refactorExtractType78.ts
Normal 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 };`,
|
||||
});
|
||||
18
tests/cases/fourslash/refactorExtractType79.ts
Normal file
18
tests/cases/fourslash/refactorExtractType79.ts
Normal 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;`,
|
||||
});
|
||||
24
tests/cases/fourslash/refactorExtractType80.ts
Normal file
24
tests/cases/fourslash/refactorExtractType80.ts
Normal 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;
|
||||
}`,
|
||||
});
|
||||
17
tests/cases/fourslash/refactorExtractType81.ts
Normal file
17
tests/cases/fourslash/refactorExtractType81.ts
Normal 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;`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType82.ts
Normal file
20
tests/cases/fourslash/refactorExtractType82.ts
Normal 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;`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType83.ts
Normal file
20
tests/cases/fourslash/refactorExtractType83.ts
Normal 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;`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType84.ts
Normal file
20
tests/cases/fourslash/refactorExtractType84.ts
Normal 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;`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType85.ts
Normal file
20
tests/cases/fourslash/refactorExtractType85.ts
Normal 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;`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType86.ts
Normal file
20
tests/cases/fourslash/refactorExtractType86.ts
Normal 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;`,
|
||||
});
|
||||
18
tests/cases/fourslash/refactorExtractType87.ts
Normal file
18
tests/cases/fourslash/refactorExtractType87.ts
Normal 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 };`,
|
||||
});
|
||||
22
tests/cases/fourslash/refactorExtractType88.ts
Normal file
22
tests/cases/fourslash/refactorExtractType88.ts
Normal 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 }> {}`,
|
||||
});
|
||||
20
tests/cases/fourslash/refactorExtractType_js9.ts
Normal file
20
tests/cases/fourslash/refactorExtractType_js9.ts
Normal 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;`,
|
||||
});
|
||||
Reference in New Issue
Block a user