[英]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.