microsoft / microsoft/TypeScript
Improve error position/range for arrow functions with expression body
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔍 Search Terms
- arrow function
- return type
- type error position/range
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
For arrow functions with an expression body, the position of the return type error could be adjusted so it doesn't cover the entire expression. This would make it easier to identify more meaningful type errors that exist within the expression.
📃 Motivating Example
Consider the following example where we're using the pipe function inside arrow functions and the add function is missing.
declare const pipe: <A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C) => C;
// declare const add: (x: number) => (n: number) => number;
declare const identity: <T>(x: T) => T;
// Arrow function with block body
const f = (): number => {
return pipe(1, add(1), identity);
};
// Arrow function with expression body
const g = (): number => pipe(1, add(1), identity);
Both of these arrow functions have two type errors:
- For the return value: Type 'unknown' is not assignable to type 'number'.
- Cannot find name 'add'.
The first type error can be fixed by addressing the second type error.
When the arrow function has a block body, these two error messages are highlighted separately:
However, when the arrow function has an expression body, the range of the return type error covers the entire expression:
This makes it very difficult to spot the inner type error for Cannot find name 'add', especially in more advanced examples.
To help with this I was wondering if we could move the position of the return type so it doesn't cover the entire expression. Perhaps it could be positioned on the => that appears immediately before the expression? This would be closer to the behaviour of arrow functions with body blocks.
Note this issue does not occur when we're not using the pipe function. I believe this is because TypeScript treats the return type of add(1) as any, whereas with pipe the type argument B will be inferred as unknown (which is desired in other cases).
const g2 = (): number => identity(add(1)(1));
💻 Use Cases
See above.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non viene indicato alcun file sorgente né alcun test. Inizia compilando l’esempio motivante con funzioni freccia con corpo di espressione e corpo a blocco, quindi segui il punto in cui viene prodotto l’intervallo della diagnostica del tipo restituito. Il lavoro è completato quando l’errore sul tipo restituito del corpo di espressione non copre più l’intera espressione, mentre l’errore interno relativo al nome mancante continua a essere segnalato separatamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100