microsoft / microsoft/TypeScript
Allow inline 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
From #5731
if (<foo is Element>(foo.nodeType === 1)) {
// Assume foo is Element here
}
A proposal from @sandersn: https://github.com/Microsoft/TypeScript/issues/5731#issuecomment-162586789
Type Predicate Expressions
A type predicate expression allows you to narrow a union type with a single expression. The syntax is
<variableistype>boolean-expressionWhen the expression is used in a conditional context, the true branch of the conditional narrows variable to type, while the false branch narrows variable by removing type.
The type predicate expression is of type Boolean, with apparent type of type predicate. The right hand side must be an expression of type Boolean. The left hand side's variable must be a name bound in the current scope. The left hand side's type must be a name bound in the current scope.
Open questions
- How does variable capture work? Can a type predicate expression be returned from a function and used to narrow a variable that's no longer in scope? This could be defined to cause an error, but that restriction is neither obvious nor easy to use.
- Why reuse the type assertion syntax? The type of the right expression is checked to be Boolean, unlike assertions, and the resulting type is still Boolean, also unlike assertions.
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 leggendo la proposta collegata dall’issue #5731 e la sezione Type Predicate Expressions qui. Risolvi le domande elencate relative alla cattura delle variabili, alla sintassi e al controllo Boolean prima di procedere; il lavoro è completo quando la sintassi del predicato inline e il relativo comportamento di narrowing nei rami true e false sono specificati e supportati per l’esempio.
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à
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100