admin管理员组文章数量:1302520
If I have the following basic function typescript
can infer the return type automatically.
function myFunction(x: number, y: number) {
return x * y;
}
Is it only useful to declare return types if typescript
cannot infer the return type because some other call is leaking any
and so it cannot make a proper inference?
function myFunction(x: number, y: number) {
return x * y || callThatReturnsAny();
}
In this case I would want to type it if I know callThatReturnsAny()
returns a number
function myFunction(x: number, y: number): number {
return x * y || callThatReturnsAny();
}
Although the best solution would just be to type callThatReturnsAny()
so that typescript
can make the inference? But in that case when should you really ever use explicit return types?
If I have the following basic function typescript
can infer the return type automatically.
function myFunction(x: number, y: number) {
return x * y;
}
Is it only useful to declare return types if typescript
cannot infer the return type because some other call is leaking any
and so it cannot make a proper inference?
function myFunction(x: number, y: number) {
return x * y || callThatReturnsAny();
}
In this case I would want to type it if I know callThatReturnsAny()
returns a number
function myFunction(x: number, y: number): number {
return x * y || callThatReturnsAny();
}
Although the best solution would just be to type callThatReturnsAny()
so that typescript
can make the inference? But in that case when should you really ever use explicit return types?
- 1 Did you mean to use binary or? – Jared Smith Commented Dec 7, 2018 at 0:31
- @JaredSmith yes, thank you. – Adam Thompson Commented Dec 7, 2018 at 0:34
- It is never necessary, you can declare it whenever you like. – zerkms Commented Dec 7, 2018 at 0:38
- @zerkms just to be more explicit as a style decision? Otherwise, I know it is not necessary - but does is there ever a good use case for doing so? – Adam Thompson Commented Dec 7, 2018 at 0:42
- 1 if you use --noImplicitAny piler option it will give an error each time you use explicit any like in callThatReturnsAny; than you can type function with any type have the safe code and save some typing on the function where the piler can infer types – setdvd Commented Dec 7, 2018 at 3:34
1 Answer
Reset to default 11I switch on noImplicitAny
and avoid adding type annotations in almost all cases, except functions. Why? Because I don't want to accidentally return a union type when:
- I forget to return a value
- I return a value of the wrong type
For example, my day goes differently if I start off with:
function example(a: number, b: number) {
vs
function example(a: number, b: number): number {
Here's what happens next...
function example(a: number, b: number) {
if (a > 5) {
return 5;
}
if (b > a) {
return 'b';
}
}
My return type is now number | string | undefined
.
If I use the return type annotation, I get additional help*.
It helps you return the correct type:
In strict mode, it makes sure you return something every time.
* if you like additional help, you'll also have all the strict things switched on.
本文标签: javascriptWhen is it necessary to declare return function type in TypeScriptStack Overflow
版权声明:本文标题:javascript - When is it necessary to declare return function type in TypeScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741684053a2392339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论