Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference index.d.ts types in package.json #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

peterharding
Copy link

As recommended in http://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html,
the package.json should contain a reference to the type declarations file index.d.ts.

In addition to being best practice, it avoids some potential problems:

Without this reference, someone could have a project with the following tsconfig.json:

"compilerOptions": {
    "moduleResolution": "node",
    "strictNullChecks": true
  },
  "exclude": [
    "node_modules"
  ]

Currently, if any file in their project has import { deserialize } from "json-typescript-mapper";, then json-typescript-mapper/index.ts will not be excluded by tsc. Therefore, when compiling their project with strictNullChecks: true, json-typescript-mapper/index.ts will be be incorrectly checked by tsc, and generate the following errors:

node_modules/json-typescript-mapper/index.ts(133,48): error TS2345: Argument of type '(new () => {}) | undefined' is not assignable to parameter of type 'new () => {}'.
  Type 'undefined' is not assignable to type 'new () => {}'.
node_modules/json-typescript-mapper/index.ts(163,9): error TS2322: Type 'undefined' is not assignable to type 'T'.
node_modules/json-typescript-mapper/index.ts(170,9): error TS2322: Type 'undefined' is not assignable to type 'T'.

Adding the index.d.ts reference to package.json allows tsc to correctly ignore json-typescript-mapper.

@peterharding
Copy link
Author

Just noticed that http://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html has:

Also note that if your main declaration file is named index.d.ts and lives at the root of the package (next to index.js) you do not need to mark the "types" property, though it is advisable to do so.

Strangely, this must not be working for me, since adding the types reference prevented my compile errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant