Featured image of post Untyped Imports in TypeScript

Untyped Imports in TypeScript

Overview

TypeScript 2.1 makes it a lot easier to work with untyped imports. Previously, the compiler was overly { 过度地 } strict and would give you an error when you imported a module that doesn’t ship with type definitions:

TypeScript error: untyped import

Starting with TypeScript 2.1, the compiler will no longer complain if there are no type declarations for a module. TypeScript is happy with untyped modules and your editor doesn’t render red squigglies anymore:

Untyped imports work just fine in TypeScript 2.1

Now, the imported range function is typed as any. The upside { 好的一面,优势 } to this is that migrating an existing JavaScript project to TypeScript should lead to fewer compile-time errors. The downside { 缺点,不利方面 } is that you won’t get any autocompletion suggestions or fine-grained type checking since the compiler doesn’t know anything about the module or its exports.

If you later provide type declarations, e.g. via a type declaration package from npm, they will take priority over the default any type. (Otherwise, there would be no way to provide types for imported modules.)

Note that untyped imports will still be flagged as errors if you’re compiling your project with the noImplicitAny option set to true — after all, the imports are now implicitly typed as any. To make the error go away { If you go away, you leave a place or a person’s company }, you can either provide type declarations or set the noImplicitAny compiler option to false.

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy