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
π₯ 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:
- Created 4 years ago
- Comments:8 (5 by maintainers)
@gkalpak @kapunahelewong I think we can close this issue
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.