概述:--我正在构建一个TypeScript声明-- NPM/GitHub软件包库(https://github.com/jefelewis/typescript-declarations),希望导入到我的项目中,但导入到项目时遇到了问题。
项目:
import { ICoordinates } from 'typescript-declarations';错误:
Module '"typescript-declarations"' has no exported member 'ICoordinates'.我尝试过的:
package.jsontsonfig.jsontype导出了src/types/locationTypes.ts和interface可能的选项:
index.ts (如https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#-reference-ing-a-module )中使用引用?src/index.ts
// Imports: TypeScript Types
import * as locationTypes from './types/locationTypes';
import * as measurementTypes from './types/measurementTypes';
// Exports
export {
locationTypes,
measurementTypes,
};src/types/locationTypes.ts:
// Module: Location Types
declare module locationTypes {
// TypeScript Type: Latitude
export type TLatitude = number;
// TypeScript Type: TLongitude
export type TLongitude = number;
// TypeScript Type: Coordinates
export interface ICoordinates {
latitude: TLatitude;
longitude: TLongitude;
}
}
// Exports
export default locationTypes;src/types/measurementTypes.ts:
// Module: Measurement Types
declare module measurementTypes {
// TypeScript Type: Measurement (Distance)
export type TMeasurementDistance = 'Kilometers' | 'Miles' | 'Yards' | 'Feet';
// TypeScript Type: Measurement (Length)
export type TMeasurementLength = 'Inches' | 'Feet' | 'Yards' | 'Miles' | 'Light Years' | 'Millimeters' | 'Centimeters' | 'Meters' | 'Kilometers';
// TypeScript Type: Measurement (Speed)
export type TMeasurementSpeed = 'Centimeters Per Second' | 'Meters Per Second' | 'Feet Per Second' | 'Miles Per Hour' | 'Kilometers Per Hour' | 'Knots' | 'Mach';
// TypeScript Type: Measurement (Angle)
export type TMeasurementAngle = 'Degrees' | 'Radians' | 'Milliradians';
}
// Exports
export default measurementTypes;发布于 2021-06-27 08:58:38
类型定义和包构建管道存在一些问题。
cd进入src目录来运行tsc。在您的tsconfig中已经描述了哪些文件应该包含在编译中。cp命令格式错误。cp需要第二个参数。要知道要将源文件复制到的目标文件/目录,请执行以下操作。尽管我相信你不需要在任何地方复制package.json。根目录中的一个很好地描述了您的npm包。不过,如果您有一些我不知道的精心编写的包发布脚本,那么这肯定不是真的。declare module someModule)。这是一个非常古老的遗留特性,现在用namespaces取代了类型记录。考虑到您是如何导入ICoordinates的,我假设您也不需要名称空间。locationTypes作为export { locationTypes },您正在创建另一个来自locationTypes.ts类型的名称空间locationTypes。总之,您仍然可以将ICoordinates导入如下:
import { locationTypes } from 'typescript-declarations'
const coords: locationTypes.default.ICoordinates = null as any不过,如果您希望导入与以下类型相同的类型:
import { ICoordinates } from 'typescript-declarations'您必须对代码进行一些调整:
// --- src/types/locationsTypes.ts
// TypeScript Type: Latitude
export type TLatitude = number;
// TypeScript Type: TLongitude
export type TLongitude = number;
// TypeScript Type: Coordinates
export interface ICoordinates {
latitude: TLatitude;
longitude: TLongitude;
}
// --- src/index.ts
// re-export all members of `locationTypes`
export * from './types/locationsTypes'如果您想在没有显式命名空间的情况下导入它们,measurementTypes也是如此。
https://stackoverflow.com/questions/68147021
复制相似问题