microsoft / microsoft/TypeScript
Accept de-structured elements in type predicates
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
- type predicate
- reference binding pattern
- type predicate cannot reference
- destructured
Suggestion
The possibility to use destructured parameters in type predicates.
Use Cases
Destructuring is heavily used in functional/reactive programming, notably with rxjs where various contextual properties tend to be passed between each operator.
Having the ability to succinctly test for types would make the code more readable, e.g.:
type Example = {
a: number;
b: string | undefined;
};
const example: Example = {
a: 42,
b: 'hello';
};
of(example).pipe(
guard(({ b }): b is string => b !== undefined, 'b cannot be undefined'),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Right now the alternative is
of(example).pipe(
guard((x): x is Omit<typeof x, 'b'> & { b: string } => x.b !== undefined, 'b cannot be undefined'),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Or, without a predicate
of(example).pipe(
map(x => {
if (x.b === undefined) {
throw new Error();
}
return x;
}),
tap({ b }) => { /* b is now a string rather than a string | undefined })
);
Examples
function assertSomething(
{ property }: T
): property is AssertionHere {
return true;
}
This would roughly translate to something like:
function assertSomething(
obj: T
): obj is Omit<T, 'property'> & { property: AssertionHere } {
return true;
}
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
Nell’issue non sono identificati file, test o punti di ingresso del compilatore. Inizia leggendo gli esempi di type-predicate e destructured-parameter, quindi traccia il modo in cui TypeScript convalida attualmente i tipi dei parametri e del valore restituito dei predicati di tipo. Il lavoro è completato quando i parametri destrutturati nei predicati di tipo vengono accettati e i relativi tipi ristretti si comportano come mostrato, senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100