Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install TypeScript declarations for scoped/namespaced packages via @types?

I'm using TypeScript and I want to use a scoped package (e.g. @foo/bar, @babel/core, etc.) that doesn't ship its own type declarations.

I've tried to run something like

npm install @types/@foo/bar

but it doesn't seem to be available.

Is there any way to get these .d.ts for these packages into the @types scope? Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

like image 843
Daniel Rosenwasser Avatar asked Nov 14 '17 23:11

Daniel Rosenwasser


People also ask

How do I import TypeScript types?

To import a type from another file in TypeScript: Export the type from file A , e.g. export type Employee = {} . Import the type in file B as import { Employee } from './another-file'; . Use the type in file B .

What is declare namespace in TypeScript?

The namespace is a way which is used for logical grouping of functionalities. It encapsulates the features and objects that share common relationships. It allows us to organize our code in a much cleaner way. A namespace is also known as internal modules.

What is type declaration in TypeScript?

TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and properties of built-in types like string or function , top-level names like Math and Object , and their associated types.


1 Answers

Is there any way to get these .d.ts for these packages into the @types scope?

Yes there is a way! It's just a little unintuitive.

npm doesn't permit scoped packages to contain @ in their name, so these names are mangled to use two underscores in place of the @.

So as an example, if you want to install type declarations for the package @foo/bar, you'll need to run

npm install @types/foo__bar

Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

Yes! From the Definitely Typed README.md:

Types for a scoped package @foo/bar should go in types/foo__bar. Note the double underscore.

like image 76
Daniel Rosenwasser Avatar answered Oct 08 '22 00:10

Daniel Rosenwasser