microsoft / microsoft/TypeScript

Create subtype reduction rules for assertion functions

Aperta
#53,963 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Experimentation Needed Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

inference assert

🕗 Version & Regression Information
⏯ Playground Link

Playground link with relevant code

💻 Code
function Test<X extends any>(params: X[]) {}
function Test2<InParams extends readonly any[]>(params: InParams) {}

function f1(a: any): number {return 1}
function f2(a: any): string {return "string"}
function f3(a: any) {return {k: 1}}

function a1(a: any): asserts a is number {}
function a2(a: any): asserts a is string {}
function a3(a: any): asserts a is {k: number} {}

const aifwe = [f1, f2, f3]
Test([f1, f2, f3])
Test([a1, a2, a3])
Test2([f1, f2, f3])
Test2([a1, a2, a3])
🙁 Actual behavior

The Test and Test2 calls are inferred as (in order):

  • function Test<((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; })>(params: (((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]): void
  • function Test<(a: any) => asserts a is number>(params: ((a: any) => asserts a is number)[]): void
  • function Test2<(((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]>(params: (((a: any) => number) | ((a: any) => string) | ((a: any) => { k: number; }))[]): void
  • function Test2<((a: any) => asserts a is number)[]>(params: ((a: any) => asserts a is number)[]): void
🙂 Expected behavior

For the assert calls to also be inferred as unions.

This is also irregular since with the wrong inference, I would expect the other functions to be marked as errors, since they have different type signatures.

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

Inizia con l'esempio collegato di TypeScript Playground e traccia l'inferenza generica per gli array di funzioni di asserzione, concentrandoti sulle regole di riduzione dei sottotipi richieste. Il lavoro è completato quando le chiamate di asserzione di Test e Test2 inferiscono unioni come le chiamate di funzioni ordinarie, con il comportamento di inferenza rilevante coperto da un test di regressione.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.