【问题标题】:Error: Module '"typescript-declarations"' has no exported member 'ICoordinates'错误:模块 '"typescript-declarations"' 没有导出的成员 'ICoordinates'
【发布时间】:2021-09-09 19:03:35
【问题描述】:

概述: 我正在构建一个 TypeScript 声明 NPM/GitHub Packages 库 (https://github.com/jefelewis/typescript-declarations),我想将它导入到我的项目中,但是我在导入到项目中时遇到了问题。

项目:

import { ICoordinates } from 'typescript-declarations';

错误:

Module '"typescript-declarations"' has no exported member 'ICoordinates'.

我的尝试:

  • 我试过修改package.json
  • 我试过修改tsonfig.json
  • 我已经从src/types/locationTypes.ts 导出了typeinterface

可能的选择:

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;

【问题讨论】:

    标签: typescript types typescript-typings


    【解决方案1】:

    您的类型定义以及包构建管道存在一些问题。

    • 您不必将cd 放入src 目录即可运行tsc。您的tsconfig 中已经描述了哪些文件应该包含在编译中。

    • 您的 cp 命令格式错误。 cp 需要第二个参数。知道要将源文件复制到的目标文件/目录。虽然我相信你不需要在任何地方复制package.json。根目录中的那个描述你的 npm 包就好了。虽然如果你有一些我不知道的详细的包发布脚本,那肯定不是真的。

    • 在您的类型定义中有不必要的内部模块 (declare module someModule)。这是 typescript 的一个非常古老的遗留功能,现在被 namespaces 取代。考虑到您尝试导入 ICoordinates 的方式,我假设您也不需要命名空间。

    • exporting locationTypes as export { locationTypes } 你正在创建另一个命名空间locationTypes 你来自locationTypes.ts 的类型现在在下面。

    总而言之,您仍然可以将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 也是如此。

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2019-06-04
      • 2017-03-29
      • 2020-11-29
      • 2019-11-28
      • 2018-11-11
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多