Node.js 初始化依赖项时,Typescript别名无法正常工作
我已经成功地在ts-config.json中实现了导入别名,在初始化包含的文件之前,编译器不会抱怨任何事情。下面是我的代码的分解: 回购交易Node.js 初始化依赖项时,Typescript别名无法正常工作,node.js,typescript,Node.js,Typescript,我已经成功地在ts-config.json中实现了导入别名,在初始化包含的文件之前,编译器不会抱怨任何事情。下面是我的代码的分解: 回购交易 import ProductRepository from '@build/repos/product'; // this works fine import ProductModel from 'models/ProductModel'; export default class GetProductRepository { static as
import ProductRepository from '@build/repos/product'; // this works fine
import ProductModel from 'models/ProductModel';
export default class GetProductRepository {
static async run(context: ProductRepository, params: Object) {
context.logger.debug('GetProductRepository CUSTOM');
// AS SOON AS I TRY TO DO THIS IS DOESN'T FIND THE MODULE
const product = new ProductModel(context.logger);
return {...};
}
}
ProductModel.ts
import Logger from '@build/base/Logger';
import BaseModel from '@build/base/BaseModel';
import { ProductDTO } from 'dtos/ProductDTO';
export default class ProductModel extends BaseModel {
logger: Logger;
productId: string;
creationDate: string;
modifyDate: string;
title: string;
price: number;
isAvailable: boolean;
hasSale: boolean;
salePrice: number;
category: string;
description: string;
mainImage: string;
galleryImages: Array<any>;
constructor(logger: Logger) {
super(logger);
this.logger = logger;
}
serialize(dto: ProductDTO) {
this['productId'] = dto['productId'];
this['creationDate'] = dto['creationDate'];
this['modifyDate'] = dto['modifyDate'];
this['title'] = dto['title'];
this['price'] = dto['price'];
this['isAvailable'] = dto['isAvailable'];
this['hasSale'] = dto['hasSale'];
this['salePrice'] = dto['salePrice'];
this['category'] = dto['category'];
this['description'] = dto['description'];
this['mainImage'] = dto['mainImage'];
this['galleryImages'] = dto['galleryImages'];
}
deserialize(data: any) {
let out: ProductDTO = data;
this.logger.debug('ProductDTO:deserialize - RETURN: ' + JSON.stringify(out, null, 4));
return out;
}
}
错误:
Error: Cannot find module 'models/ProductModel' // THIS ONLY HAPPEN'S WHEN I UNCOMMENT "const product = new ProductModel(context.logger);" IN THE MAIN REPO FILE
Error: Cannot find module 'models/ProductModel' // THIS ONLY HAPPEN'S WHEN I UNCOMMENT "const product = new ProductModel(context.logger);" IN THE MAIN REPO FILE