microsoft / microsoft/TypeScript
Suggestion: Type annotations and interfaces for function declarations
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Currently in TypeScript, function declarations cannot be typed in the same way as function expressions, e.g. this function can implement the React.FC interface:
interface TestProps {
message: string
}
const Test: React.FC<TestProps> = ({ message }) => {
return <div>{message}</div>
}
But this function can't, at least not directly:
function Test({ message }: TestProps) {
return <div>{message}</div>
}
This becomes more of an issue if you try to add properties to the function object:
Test.defaultProps = {
message: 'hi'
}
It seems that currently the only way to specify the type for the function object in the second example is to create a new variable:
const AliasForTest: React.FC<TestProps> = Test
AliasForTest.defaultProps = {
message: 'hi'
}
This seems like kind of an ugly workaround, so it seems that the current idiom is to just prefer arrow functions for cases like this. But this leads to inconsistency on teams that generally prefer function declarations over const expressions for top-level functions. Personally I find it more readable to see the word "function" for top-level functions rather than seeing "const", which is generally already all over the place in the code. There is even an ESLint rule for teams that share my preference (although I don't think it's been ported to TSLint yet): https://eslint.org/docs/rules/func-style. In any case, I have seen others express similar views and other codebases (including some from Facebook and Apollo, for example) that still prefer the "function" keyword for top-level functions.
However, stylistically it's also a problem if top-level functions are declared some places as declarations (using function) and in other places as expressions (using const). But for those who desire consistency, TypeScript is basically forcing the use of expressions, due to the issues described above.
This is far from being a top priority of course, but I was surprised to see that TypeScript didn't provide some equivalent typing syntax for function declarations. It would be great if this could be considered for a future version (even if far in the future). Thanks for reading!
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es werden keine Repository-Dateien oder Tests genannt. Beginne damit zu prüfen, wie TypeScript derzeit Funktionsdeklarationen im Vergleich zu Ausdrücken typisiert, und kläre anschließend die gewünschte Deklarationssyntax und ihr Verhalten für Eigenschaften wie defaultProps. Als abgeschlossen gilt die Aufgabe, wenn ein abgestimmtes Design sowie eine von den Maintainer akzeptierte Implementierung und Tests vorliegen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100