Repository: TypeScript
Issue: TS refactoring: Infer type to a type alias
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

infer type to a type alias
code refactorings
code actions
extract type

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->
When invoking JS/TS refactorings (<kbd>ctrl</kbd><kbd>.</kbd>) **on a variable declaration/assignment** show an action to
- Infer type from RHS to a type alias and use that alias to type that variable.
- OR if the type already has a type alias, use it to type the variable right away.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->
Makes the code safer and more explicit by giving the developer control over the internally used inferred type.

## 💻 Use Cases

### 1. inferred type

before:
```ts
  const json = {
    description: '<DESCRIPTION>',
    created_by: '<CONTRACT_OWNER>',

    name: `<NAME> #${nft._id}`,
    image: `<IMAGE_IPFS_URL>${nft._id}<IMAGE_EXTENSION>`,
    image_details: {
      bytes: '<IMAGE_BYTES:number>',
      format: '<IMAGE_FORMAT>',
      sha256: '<IMAGE_SHA>',
      width: '<IMAGE_WIDTH:number>',
      height: '<IMAGE_HEIGHT:number>',
    },

    attributes: categoryNames.map(category => ({
      'trait_type': category,
      'value': nft[category].trait,
    })),
  }
```

after:
```ts
  interface Json { // <- this interface is created by the code action
    description: string
    created_by: string
    name: string
    image: string
    image_details: {
      bytes: string
      format: string
      sha256: string
      width: string
      height: string
    }
    attributes: {
      trait_type: keyof typeof traitsList
      value: string
    }[]
  }

  const json: Json = { // <- immediately used here
  // ... same as before
  }
```


### 2. existing type

before:
```ts
interface Something {
  one: number
  two: number
}
const DEFAULT_SOMETHING: Something = {
  one: 1,
  two: 2,
}

function doSomething() {
  const something = Object.assign({}, DEFAULT_SOMETHING)

  // do something with something
  return something
}
```

after:
```ts
function doSomething() {
  const something: Something = Object.assign({}, DEFAULT_SOMETHING)
```

I could copy the type manually from the popup on hover, but it only provides so much information, so the type may not be complete.

![image](https://user-images.githubusercontent.com/2313018/162597124-e8788991-a81c-48c1-8c2a-8ddf1648342b.png)


Funnily enough, I have just discovered that when I return it from a function, I can use action `Infer function return type` and receive the full type. What a hack! 😂
It gives me the full type properly, although little awkwardly on a single line of code.

![vscode-infer-fn-type](https://user-images.githubusercontent.com/2313018/162597230-7bbaaa39-569b-4e04-8cb2-14f54fb9fcac.gif)