- 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.
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- 趣學Python算法100例
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- 好好學Java:從零基礎到項目實戰
- Python語言實用教程
- Learning YARN
- Couchbase Essentials
- JavaScript動態網頁編程
- 運維前線:一線運維專家的運維方法、技巧與實踐
- HTML+CSS+JavaScript編程入門指南(全2冊)
- 硬件產品設計與開發:從原型到交付
- Mastering jQuery Mobile
- 從零開始:C語言快速入門教程
- Practical Maya Programming with Python
- Swift High Performance