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

Refactoring: Extract to interface #31640

Closed
DanielRosenwasser opened this issue May 29, 2019 · 3 comments · Fixed by #31644
Closed

Refactoring: Extract to interface #31640

DanielRosenwasser opened this issue May 29, 2019 · 3 comments · Fixed by #31644
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 29, 2019

The "extract to type alias" code fix is great, however, a lot of people (myself included) prefer interfaces for object types rather than type aliases.

Extract to interface should work only on an intersection type with some number of anonymous object types, named object types, and type references.

Examples

// before
let x: { hello: string, world: string };

// after
interface NewType {
    hello: string;
    world: string;
}

let x: NewType;
// before
let x: { hello: string, world: string } & Record<string, string>;

// after
interface NewType extends Record<string, string> {
    hello: string;
    world: string;
}

let x: NewType;

Related issues:

@DanielRosenwasser DanielRosenwasser added Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript labels May 29, 2019
@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented May 29, 2019

P.S. it's okay if the refactoring results in a type that doesn't actually type-check as long as it does "the right thing" 90% of the time - don't over-do it (e.g. running on { hello: number } & Record<string, string>)

@Kingwl
Copy link
Contributor

Kingwl commented May 29, 2019

Extract to interface should work only on an intersection type with some number of anonymous object types, named object types, and type references.

I prefer deny refactor if there are any type reference(and not a type literal) in the selection

@DanielRosenwasser
Copy link
Member Author

That's fine (and understandable) - I think it's a reasonable first step for the refactoring, and it would likely hit most of the cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants