microsoft / microsoft/TypeScript

Deprioritise properties of the form `{ prop?: never }` in completions

Aperta
#62,024 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔍 Search Terms

exact union properties, autocomplete prioritisation, completion prioritisation, `never` properties

### ✅ Viability Checklist

- [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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

It'd be nice if properties of the form `{ prop?: never }` were deprioritised in autocomplete. For example in this snippet:
```ts
const data: { a?: never; b: string } = { b: "foo" };
```

If you write `data.` you'll immediately get suggested `a` and then `b` but the property `a` may as well be useless in practice.

Similarly properties of the form `{ prop?: undefined }`, `{ prop: undefined }`, and `{ prop: never }` _could_ also be deprioritised. `{ prop?: undefined }` makes sense for better support for `"exactOptionalPropertyTypes": false` and the required variants are simply for consistency.

### 📃 Motivating Example

TypeScript assumes all objects can have excess keys. That can be a source of common confusion when snippets like this doesn't work:
```ts
function getData(): { a: string } | { b: string } {
return { a: "foo" };
}

getData().a;
// ^ Property 'a' does not exist on type '{ a: string; } | { b: string; }'.
// Property 'a' does not exist on type '{ b: string; }'.
```

Users generally expect this to simply be typed as `string | undefined`. A common pattern for overcoming this is adding properties like `{ b?: never }`. So common, in fact, that TypeScript itself produces it:
```ts
const data = Math.random() > 0.5 ? { a: "foo" } : { b: "bar" };
// ^ { a: string; b?: never } | { b: string; a?: never }
```
(if you see `b?: undefined` and `a?: undefined` that's because you don't have `exactOptionalPropertyTypes` enabled).

However the property completions are worsened due to this.
```ts
const data = Math.random() > 0.5 ? { a: "foo" } : { b: "bar" };

if (data.b === "foo") {
// `data` has been narrowed due to the above condition being impossible for `{ a: string; b?: never }`
data;
// ^ `{ a?: never; b: string }`

data.
// ^ `a` is suggested first despite being a completely useless property.
}
```

### 💻 Use Cases

1. What do you want to use this for?
Narrowed unions with `never` properties.
2. What shortcomings exist with current approaches?
Poor completion ordering.
3. What workarounds are you using in the meantime?
No workarounds are possible today as far as I know because the only control a user has over completion ordering is a property name.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci gli esempi di completamento `data.` dell’issue, inclusi l’unione ristretta e le varianti di `exactOptionalPropertyTypes`. Traccia il percorso di prioritizzazione del completamento per le proprietà tipizzate come `never` o `undefined` e considera il lavoro completato quando tali proprietà vengono sistematicamente declassate senza modificare il controllo dei tipi o il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
developer-experience, tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.