microsoft / microsoft/TypeScript-Sublime-Plugin

Feature request: Could the plugin recommend "import"s for inferred types?

Abierto
#737 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
JavaScript
Estrellas
1.7k
Forks
236
Merge medio
21 d 13 h
PR fusionados (30 d)
1

Descripción

Try opening this minimal demo project https://github.com/mcclure/ts-hello-bug/tree/editor-demo in Sublime Text, and opening the file 'src/index.tsx'. index.tsx imports a function demoFunction() from 'dependencyOne.ts' and calls it. If I move the cursor over the let x in 'index.tsx', it says:

let x : DemoClass, Line 3, Column 5

Here is a problem: what is DemoClass? I didn't import any such class. If I try to say let x : DemoClass = demoFunction(), I will get error TS2304: Cannot find name 'DemoClass'., because I didn't import it. (Since I wrote the demo project myself, I know the answer: 'dependencyOne.ts' imported it from 'dependencyTwo.ts'. But let's say I don't know that.)

If I cmd-P and look at the features of the TypeScript plugin, I see it exposes a useful function: "GoTo Type Definition". If I select this option, it takes me to class DemoClass in 'dependencyTwo.ts'. Awesome, problem solved! I need to import { DemoClass } from "./dependencyTwo"`.

But consider a slightly more complicated example: What if DemoClass had been imported from a library? In this case the existing tools are less helpful. Try out this other demo project https://github.com/mcclure/ts-hello/tree/stable which is also minimal, but this one has Preact in package.json. Try npm installing, then opening 'src/index.tsx' and adding the line:

let test = <div>Test</div>

If I mouse over test it says "let test : h.JSX.Element". This is a little odder. If I "GoTo Type Definition" this, I get:

interface Element extends preact.VNode<any> {}

inside namespace JSXInternal, in jsx.d.ts. That's not actionable the way finding 'dependencyTwo.ts' was. What I want to do here, probably, is import { JSX } from "preact"; and then use the type JSX.Element. But I wouldn't figure this out by using the Sublime plugin.

I'm not sure these are the best examples. But this is a situation I did find myself in twice this weekend: There is a symbol or value I have access to by writing code. The Sublime plugin can successfully infer its type. I want to assign the value to a variable, and I want to write a type signature for that variable. But the type is not in scope, I have no idea what to import to get it in scope, and the plugin can't help me because the type is something deep inside a spaghetti of import exports in 'node_modules'.

Expected behavior/suggestion: In both the cases above, I think the plugin could have figured out the correct import line and suggested it to me. In the first case, it could have followed the import from './dependencyTwo' and detected I can import './dependencyTwo' also. In the second case, it could have noticed the JSXInternal namespace that h import exports is also import exported by the "preact" node_module (which I am already importing other symbols from) and suggested that.

(The second example might be a harder case because it would require the plugin to speculate about different ways JSXInternal could get into scope; but possibly if there are several sensible "import" routes, the plugin could show a list. Also I think the plugin has this speculation logic already, because if I type let test : JSX.Element without importing JSX, then use the mysterious "TypeScript: Request Code Fixes" feature of the plugin, it magically adds , JSX to my "import JSX" line up top (!))

I am using v3.7.3 to test.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce las dos demos de src/index.tsx, incluido el caso de las dependencias locales dependencyOne.ts/dependencyTwo.ts y el caso de Preact descrito en jsx.d.ts. Compara GoTo Type Definition con TypeScript: Request Code Fixes y define como finalizado que se sugieran imports utilizables tanto para tipos locales como para tipos expuestos a través de un paquete.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, typescript
Área
developer-experience, tooling
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
32/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.