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.

Decleration file for imported JS-library

See original GitHub issue

πŸ“š Docs or angular.io bug report

Description

When following the guide on creating decleration file for imported JS-library you refer to TypeScript documentation. But no information on how to actually import a index.d.ts module file.

Files:

Js-file

function doSomething(params) {
    console.log("doesSomething ", params)
    }

index.d.ts file

declare module "functionsModule"{
    export function doSomething(param: string);
}

Component importing the declared module

import { Component, OnInit } from '@angular/core';
import {doSomething} from 'functionsModule';

@Component({
  selector: 'app-ownjsfile',
  templateUrl: './ownjsfile.component.html',
  styleUrls: ['./ownjsfile.component.scss']
})
export class OwnjsfileComponent implements OnInit {
  constructor() { }

  ngOnInit() {
    doSomething("Hello World?");
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "importNoneAngularModule": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/importNoneAngularModule",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss",
              "node_modules/pivottable/dist/pivot.css"
            ],
            "scripts": [
              "src/customJs/FunctionsFileName.js",
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/jquery-ui-dist/jquery-ui.min.js",
              "node_modules/pivottable/dist/pivot.min.js"],
            "es5BrowserSupport": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "importNoneAngularModule:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "importNoneAngularModule:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "importNoneAngularModule:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss",
              "node_modules/pivottable/dist/pivot.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/jquery-ui-dist/jquery-ui.min.js",
              "node_modules/pivottable/dist/pivot.min.js"
            ],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**",
              "src/customeTypes"
            ]
          }
        }
      }
    },
    "importNoneAngularModule-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "importNoneAngularModule:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "importNoneAngularModule:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**",
              "customeTypes"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "importNoneAngularModule"
}

tsconfig.json

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

What’s the affected URL?**

https://angular.io/guide/typescript-configuration

Reproduction Steps**

Create a new angular project and a simple js-library file. Create a index.d.ts decleration file which export a module and try to use it in a component.

Expected vs Actual Behavior**

To import the decleration as a component and use it’s exported functions, classes and variables.

πŸ“·Screenshot

bild

πŸ”₯ Exception or Error

ERROR in ./src/app/without-type/ownjsfile/ownjsfile.component.ts Module not found: Error: Can’t resolve β€˜functionsModule’ in 'C:\Workspace\angular-tests\importModules\importNoneAngularModule\src\app\without-type\ownjsfile’i ο½’wdmο½£: Failed to compile.

🌍 Your Environment

Angular: 7.2.15

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ajitsinghkalercommented, Apr 19, 2020

@gkalpak @kapunahelewong I think we can close this issue

0reactions
angular-automatic-lock-bot[bot]commented, May 21, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Library Structures - TypeScript
This section shows how to import them into the declaration file. Dependencies on Global Libraries. If your library depends on a global library,...
Read more >
A quick introduction to β€œType Declaration” files and adding ...
The standard library (global type declaration) files are imported implicitly by the TypeScript compiler by looking at the lib compiler-option (Β ...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In...
Read more >
Declaration Files | The TypeScript Workshop
In this exercise, we'll create a declaration file from scratch. We'll declare file conventions, import, and then use declared files.
Read more >
Writing a typescript declaration file for an external js nodejs ...
jinxed is a simple library that currently exports a single function via module.exports { ... }, it is not intended to be the...
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