microsoft / microsoft/TypeScript

(Proposal) syntax for contextual typing of a function declaration

Aperta
#16,918 2 commenti 25 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Currently, it is possible to give a function a contextual type if it is a function expression. This is often used by callback types, and by certain interface types that add properties, or specify the parameter types / return type of a function.

A common use, in the declaration case, is React.SFC:

interface Props {
  propName?: string
}
const Component: React.SFC<Props> = props => <div>{props.propName!}</div>
Component.defaultProps = { propName: 'propName' }

In the snippet above, props is contextually typed, and the function is only allowed to return a React.Element or null. The function object also has several keys added to it, most notably defaultProps, displayName and propTypes.

However, there is no code reason for why Component needs to be a function expression. It could as well be a function declaration. However, making it a function declaration prevents it from being typed as React.SFC.

Not being able to give it a type requires that all parameters have the types manually written, and also prevents attaching the extra property keys such as defaultProps. It will also not validate the function's return type -- you will instead get an error when you attempt to use the function if it happens to be incompatible.

The idea here is to have a way to tell the compiler what the "contextual type" of a function declaration should be.

One possible syntax could be appropriating the implements keyword used for class:

interface Props {
  propName?: string
}
Component.defaultProps = { propName: 'propName' }
function Component (props) implements React.SFC<Props> {
  return <div>{props.propName!}</div>
}

This would make it so Component uses React.SFC<Props> type as its type, similar to if it was a function expression that has a context given to it. This should have no effect on code emit and can simply be stripped on emit; similarly to the class implements clause.

In more generic terms,

function FunctionName (...ArgumentList) implements TypeExpression FunctionBody

should behave similarly to

var FunctionName: TypeExpression = function (...ArgumentList) FunctionBody

but be an actual hoisted function declaration.


Having a way of specifying the type like this also could make for easier refactoring of callbacks -- when extracting a callback from being given as an expression to being a named declaration, you often have to go to the type declaration and copy&paste the signature types to the extracted declaration.

With something like the proposed implements, you will still need to add a type, but it will only be the original context type; which then removes the need of explicitly giving types to each argument and the return type.

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 confrontando la sintassi proposta per la dichiarazione di funzioni con il comportamento esistente di tipizzazione contestuale delle espressioni di funzione descritto nell’issue. Determina le aree parser, checker, emit e test che richiederebbero una copertura; il lavoro è completato quando la sintassi fornisce la tipizzazione contestuale indicata dei parametri e del valore restituito, supporta le proprietà delle funzioni, preserva il hoisting ed emette codice aggiuntivo.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.