microsoft / microsoft/TypeScript
Narrowing `this` using custom type predicate affected by TS 5.0
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
narrowing this predicate
🕗 Version & Regression Information
- This changed between versions 4.9 and 5.0
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Typestate<TContext extends object> {
value: string;
context: TContext;
}
interface State<TContext extends object, TState extends Typestate<TContext>> {
value: TState["value"];
context: TContext;
matches: <TSV extends TState["value"]>(
value: TSV
) => this is TState extends {
value: TSV;
}
? TState & {
value: TSV;
}
: never;
}
interface Machine<TContext extends object, TState extends Typestate<TContext>> {
initialState: State<TContext, TState>;
}
export declare function createMachine<
TContext extends object,
TState extends Typestate<TContext> = {
value: any;
context: TContext;
}
>(): Machine<TContext, TState>;
const machine = createMachine<{ count: number }>();
const state = machine.initialState;
if (state.matches("idle")) {
((_accept: number) => {})(state.context.count);
// @ts-expect-error
((_accept: string) => {})(state.context.count);
} else if (state.matches("latest")) {
((_accept: number) => {})(state.context.count);
// @ts-expect-error
((_accept: string) => {})(state.context.count);
}
🙁 Actual behavior
state gets narrowed down to never in the else branch
🙂 Expected behavior
state to only get narrowed down to the appropriate value within the if branch
cc @ahejlsberg
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
Inizia con il repro collegato in Playground e confronta il suo comportamento di narrowing tra TypeScript 4.9 e 5.0. Analizza il percorso di type-checking per i predicati personalizzati su this, quindi verifica che i rami if ed else vengano ristretti a stati appropriati e che i controlli @ts-expect-error inclusi abbiano esito positivo.
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
- Specificata chiaramente
- Idoneità per principianti
- 42/100