question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add custom typings to a library build?

See original GitHub issue

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

I’m working on a project where unfortunately there are some events polluting the window object… nobody is proud of that here but that’s what we got 🙈

When running angular-cli -> ng build, the angular-cli is able to compile a specific ts file that contains a call to window.specialEvent but when running ng-packagr -p we get: Property ‘specialEvent’ does not exist on type ‘Window’. I have a local file window.d.ts that contains:

declare global {
  export interface Window {
    specialEvent(eventName: string, data: any): boolean;
  }
}

this is our tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

Expected Behaviour

Angular library is build with success 😎

Version Information

ng-packagr: ^1.3.0
node: v6.9.1
@angular/cli: 1.4.5,
@angular: ^4.2.4
rxjs: ^5.4.2,
zone.js: ^0.8.14

ps. thanks in advance for your support and thanks for this great project!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
dhergescommented, Oct 19, 2017

Hi @x1c0,

thanks for reporting.

My recommendation is to either use typings from node_modules/@types/<*> or reference a custom *.d.ts file by a triple slash refertence. The latter is a proven solution for working with custom (proprietary / legacy) libs - maybe, you even need the d.ts file by hand - at least it works well with at least one proprietary lib that I use.

6reactions
mark-at-tusksoftcommented, Jan 21, 2020

Stumbled across this from a google search. Just to clarify, once you have created the typings file (i.e. projects/lib-project/src/typings.d.ts) Be sure to add it to the types array in projects/lib-project/tsconfig.lib.ts

{
  "compilerOptions": {
    "types": [
      "./src/typings"
    ],
  }
}

This worked for me; now VSCode recognizes the namespaces referenced in typings.d.ts and the tsconfig.lib “types” addition helps ng-packagr find the custom typings file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add custom types into the TypeScript project - drag13.io
Create custom typings · Create a root folder for your types. You can give any name for it but let it be types...
Read more >
How to add custom "typings" in typescript 2.0 / 3.0
You can create local custom typings just for your project, where you can declare types for JS libraries. For that, you need to:....
Read more >
TypeScript: Adding Custom Type Definitions for Existing ...
In these circumstances, you have to add your own custom type definitions for the libraries. This article will show you how to that....
Read more >
Adding Custom Type Definitions to a Third-Party Library
Try installing it via npm or yarn. However, if it is one of the many libraries out there that doesn't have any public...
Read more >
How to add custom types to a javascript library
Create a file with extension .d.ts , generally you'll put this file on dist folder. Now here you can add your custom types....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found