In the actual project development process, it is inevitable to encounter third-party NPM modules, and these NPM modules are not necessarily written in TS, so the members it provides do not have a strongly typed experience.
import { cameCase } from 'lodash' // Could not find a declaration file from module 'lodash'. Try `npm install @types/lodash` if it exists or add a new declaration (.d.ts) file) declare function camelCase (input: CamelCase (' Hello typeScript') // (alias) camelCase(input: string):stringCopy the code
A type declaration is when a member does not declare an explicit type when it is defined, and then makes an explicit declaration for it when it is used. The reason for this usage is to consider compatibility with common JS modules, as the TS community is very strong. Most of the most common NPM modules already provide the corresponding declaration, just need to install its corresponding type declaration module.
// Install the lodash module yarn add @types/lodash -dCopy the code
Summary: Reference third-party modules in TS if the module does not contain the type declaration file to be consumed. Try to install a corresponding type declaration module in the form of @types/ module name. If there is no such type declaration module, in this case you can only declare the corresponding module type yourself using the DECLARE statement.