简体   繁体   English

如何在 Angular 中使用带有 declarations.d.ts 的 .js 文件

[英]How to use .js file with declarations .d.ts in Angular

I have Angular 7 app and I want to include some typed JS constants from outside of the project (These constants are used in the AngularJS app, so I need to keep it in js).我有 Angular 7 应用程序,我想从项目外部包含一些类型化的 JS 常量(这些常量在 AngularJS 应用程序中使用,所以我需要将它保留在 js 中)。 I defined new path in paths in tsconfig.json我在 tsconfig.json 的路径中定义了新路径

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowJs": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "baseUrl": "src",
    "paths": {
      "@app/*": ["app/*"],
      "@env/*": ["environments/*"],
      "@dep/*": ["../../web-common/*"]
    }
  }
} 

In @dep i have test.js and test.d.ts that contains following在@dep 中,我有 test.js 和 test.d.ts,其中包含以下内容

export const TEST_CONST = {
  parser: 'CSV',
  styler: 'XVE'
};

and

export interface TEST_CONST {
  parser: string;
  styler: number;
}

But when I import it and start using但是当我导入它并开始使用时

import {TEST_CONST} from '@dep/constants/test';

... ...

console.log(TEST_CONST.styler);

It gives me它给了我

error TS2693: 'TEST_CONST' only refers to a type, but is being used as a value here.错误 TS2693:“TEST_CONST”仅指类型,但在此处用作值。

I also tried to import ' @dep/constants/test.js ' but it is same.... How to import js file with typings into Angular app?我也尝试导入“ @dep/constants/test.js ”,但它是一样的....如何将带有类型的 js 文件导入 Angular 应用程序? As I understand it, this should be possible.据我了解,这应该是可能的。

Try to add declare and remove export :尝试添加declare并删除export

declare interface TEST_CONST {
  parser: string;
  styler: number;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将js文件与angular一起使用(d.ts文件) - Use js file with angular (d.ts file) 如何在Angular中使用自定义的Typescript声明文件(.d.ts)? - How do I use a custom Typescript Declaration file (.d.ts) from within Angular? Angular/webpack 不在 @spartacus/storefront 中寻找 .d.ts 文件 - Angular/webpack not looking for .d.ts file in @spartacus/storefront 如何在Angular库的node_modules文件夹之外使用* .d.ts? - How can I use a *.d.ts outside of the node_modules folder for my Angular library? 为js函数编写d.ts文件的正确方法是什么? - What is correct way to write d.ts file for js function? 如何将我的自定义.d.ts文件添加到我的角度库? - How to add my custom .d.ts file to my angular library? 如果该库没有可用的类型定义(*.d.ts)文件,如何在 angular4 中导入外部 javascript 库 - how to import external javascript library in angular4 if there is no type definition(*.d.ts) file is available for that library angular2-种子如何添加一个d.ts - angular2-seed how to add a d.ts 如何解决文件的扩展名不受支持。 唯一支持的扩展名是“.ts”、“.tsx”、“.d.ts”、“.js”、“.jsx” - How to solve File has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx' 如何从`.d.ts`文件导入? - How do I import from a `.d.ts` file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM