Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify multiple type definitions files in package.json

I'm working on creating type definitions for a node library[1] but I'm having troubles with organising the files.

I've added the typings field to the package.json and that works great, the problem that I have is that I'd like to specify multiple type definition files since the library itself is split in two entry points:

  • styled-components
  • styled-components/native

So I have created type definitions for both the entry points but I'm unsure on how to make use of the typings field correctly. This is the structure I have (I'm only including what's relevant):

├── index.js
├── native.js
├── package.json
├── typings
│   ├── styled-components-native-test.tsx
│   ├── styled-components-native.d.ts
│   ├── styled-components-test.tsx
│   └── styled-components.d.ts

In styled-components.d.ts I have the main definitions, that should be loaded be doing import styled from "styled-components". In styled-components-native.d.ts I have the definitions for the native entrypoint, that should be loaded be doing import { x } from "styled-components/native".

The typings field inside the package.json is pointing to typings/styled-components.d.ts, but this doesn't make the styled-components-native.d.ts definitions available.

I've also tried to have an index.d.ts and referencing (with the triple slash notation) the other two files, but it didn't work.

So is there a way to achieve this?

[1] here's my PR https://github.com/styled-components/styled-components/pull/152

like image 929
patrick Avatar asked Nov 15 '16 22:11

patrick


People also ask

Can you have more than one package json file?

Multiple package. json files give you a lot of flexibility to run different/incompatible versions of dependencies. As a practical example, on one of the projects that I work on we have 2 package. json files, one for the main application code and then another one for our BDD tests.

Where do I put type modules in package json?

How do I add a module to a package json? To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the –save-prod flag for dependencies (the default behavior of npm install ) or the –save-dev flag for devDependencies.

What are @types packages?

The @types scope package contains type definitions for a lot of libraries, like Express, Sequelize, JQuery, and many others.

What does Carrot mean in package json?

Using a caret (^) sign means that we can accept minor releases and patch releases, but not a major release when updating our package.


1 Answers

Try:

├── index.js
├── index.d.ts
├── native.js
├── native.d.ts

This should work correctly, as this is how packages like @angular/material bundle their types for such imports.

Basically the typescript compiler will look for a .d.ts file of the same name as the javascript file.

(Edit: sorry didn't realize this question was so old... will leave the answer here anyway in case it helps someone)

like image 116
Michael Avatar answered Oct 11 '22 18:10

Michael