- 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.
推薦閱讀
- jQuery Mobile Web Development Essentials(Third Edition)
- Building a RESTful Web Service with Spring
- JavaScript Unlocked
- 深入淺出WPF
- HTML5+CSS3基礎開發教程(第2版)
- 機器人Python青少年編程開發實例
- 精通API架構:設計、運維與演進
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Bootstrap 4:Responsive Web Design
- 從零開始學Linux編程
- Mastering Xamarin.Forms(Second Edition)
- Visual Basic程序設計教程
- Visual Studio 2015高級編程(第6版)
- Processing創意編程指南
- Django 3.0應用開發詳解