- Learn React with TypeScript 3
- Carl Rippon
- 152字
- 2021-06-10 19:16:38
--noImplicitAny
This forces us to explicitly specify the any type where we want to use it. This forces us to think about our use of any and whether we really need it.
Let's explore this with an example:
- Let's add a doSomething method to our OrderDetail class that has a parameter called input with no type annotation:
export class OrderDetail {
...
doSomething(input) {
input.something();
return input.result;
}
}
- Let's do a compilation with the --noImplicitAny flag in the Terminal:
tsc orderDetail --noImplicitAny
The compiler outputs the following error message because we haven't explicitly said what type the input parameter is:
orderDetail.ts(14,15): error TS7006: Parameter 'input' implicitly has an 'any' type.
- We can fix this by adding a type annotation with any or, better still, something more specific:
doSomething(input: {something: () => void, result: string}) {
input.something();
return input.result;
}
If we do a compilation with --noImplicitAny again, the compiler is happy.
推薦閱讀
- 一步一步學Spring Boot 2:微服務項目實戰
- LabVIEW程序設計基礎與應用
- Java異步編程實戰
- MongoDB for Java Developers
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Reactive Programming With Java 9
- Clojure Reactive Programming
- SQL Server從入門到精通(第3版)
- AutoCAD 2009實訓指導
- Geospatial Development By Example with Python
- JBoss:Developer's Guide
- Mastering Docker
- Data Science Algorithms in a Week
- Delphi開發典型模塊大全(修訂版)
- Arduino電子設計實戰指南:零基礎篇