Skip to content

Suggestion: Refactor extract to type alias/typedef #23869

Closed
@mhegazy

Description

@mhegazy

TypeScript Version: 2.9.0-dev.201xxxxx

Search Terms: Refactor, extract type, typedef

In a TypeScript file Extract to type alias:

var x: { a: number, b: string } = { .. };

would generate:

type newType = { a: number, b: string }
var x: newType = { .. };

In a JavaScript file Extract to typedef:

/** @type {import("./c2").mytype} */
var x;

would generate:

/** @typedef {import("./c2").mytype} newType */

/**@type {myType} */
var x;

gif courtesy of @DanielRosenwasser

extracttypealias

Activity

added
SuggestionAn idea for TypeScript
Domain: Refactoringse.g. extract to constant or function, rename symbol
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
Domain: JSDocRelates to JSDoc parsing and type generation
on May 3, 2018
Kingwl

Kingwl commented on Jun 11, 2018

@Kingwl
Contributor

it's very helpful to me

mhegazy

mhegazy commented on Jun 11, 2018

@mhegazy
ContributorAuthor

@Kingwl is that something you would be interested to implement as well? we would accept a PR for this one.

Kingwl

Kingwl commented on Jun 11, 2018

@Kingwl
Contributor

sure,but it going to be late,my deadline is coming😂

Kingwl

Kingwl commented on Jun 19, 2018

@Kingwl
Contributor

something need to consider:

  1. option parameter?
  2. rest parameter?
  3. update all CallExpression ?
mhegazy

mhegazy commented on Jun 19, 2018

@mhegazy
ContributorAuthor
  • For optional parameters I would strip off the |undefined from the type e.g. function f(a? :number | undefined) => type newType = number ; function f(a?: newType)
  • initializers need to be maintained, e.g. function f(a : number | string = 0) should be type newType = number | string; function f(a: newType = 0)
  • rest parameters are fine, since you are extracting their type
  • do not think you need to update call expressions..

I think you are confusing this feature with extract to named arguments. e.g. function f(a: number, b:string) => function f({a, b}: {a: number, b:string}). this one is tracked by #23552.

Kingwl

Kingwl commented on Jun 19, 2018

@Kingwl
Contributor

Yes, I confused the two feature (:sad) , this one looks like this is a relatively simple operation

Kingwl

Kingwl commented on Jun 21, 2018

@Kingwl
Contributor

could you give some advice about the new name of the newType?

mhegazy

mhegazy commented on Jun 21, 2018

@mhegazy
ContributorAuthor

dose not matter what name you pick really. it has to be unique. The new name will be the rename location for the refactoring, and thus the user will get to update it immateriality after the refactor is applied.

Kingwl

Kingwl commented on Jun 21, 2018

@Kingwl
Contributor

should it trigger with signal primitive type and extract to a type alias?

mhegazy

mhegazy commented on Jun 21, 2018

@mhegazy
ContributorAuthor

I suppose so.. any type node really should be extractable..

12 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueDomain: JSDocRelates to JSDoc parsing and type generationDomain: Refactoringse.g. extract to constant or function, rename symbolFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @DanielRosenwasser@RyanCavanaugh@Kingwl@mattwelke@mhegazy

      Issue actions

        Suggestion: Refactor extract to type alias/typedef · Issue #23869 · microsoft/TypeScript