moment-duration-format.d.ts定义不扩展矩量模块

时间:2016-12-29 16:16:57

标签: javascript typescript momentjs

知道为什么这不起作用或如何扩展持续时间接口以支持格式化功能?

declare module 'moment' {
     interface Duration {
       format(template: string, precision?: string, settings?: any): string;
     }

}

用作:

moment.duration(minutes, 'minutes').format('mm');

我收到“持续时间”

类型中“格式”不存在的错误

3 个答案:

答案 0 :(得分:7)

进口:

import * as moment from 'moment';
import 'moment-duration-format';

在课堂之外,定义接口:

interface Duration extends moment.Duration {
  format: (template?: string, precision?: number, settings?: DurationSettings) => string;
}

interface DurationSettings {
  forceLength: boolean;
  precision: number;
  template: string;
  trim: boolean | 'left' | 'right';
}

然后在你的代码中:

const duration = moment.duration(minutes, 'minutes') as Duration;
return duration.format('mm');

如果您在另一个文件中定义了Duration接口,则还需要导出和导入它。

答案 1 :(得分:6)

首先,安装类型:

npm install --save-dev @types/moment-duration-format

其次,将它们导入您的文件中:

/// <reference path='../..your-path.../node_modules/@types/moment-duration-format/index.d.ts' />
import * as moment from 'moment';
import 'moment-duration-format';

然后你可以使用

moment.duration(minutes, 'minutes').format('mm');

答案 2 :(得分:1)

您需要安装以下依赖项:

TypeError: can only concatenate str (not "NoneType") to str


~\Anaconda3\envs\myenv\lib\site-packages\azureml\core\conda_dependencies.py in add_conda_package(self, conda_package)
    461                 if conda_package.startswith(PYTHON_PREFIX):
    462                     python_version = self._get_version(conda_package)
--> 463                     self.set_python_version(python_version)
    464                 else:
    465                     self._conda_dependencies[PACKAGES].append(conda_package)

~\Anaconda3\envs\myenv\lib\site-packages\azureml\core\conda_dependencies.py in 
set_python_version(self, version)
    418                 if self._python_version != version:
    419                     # Doing an inplace update to preserve the comment above this field in the file.
--> 420                     self._conda_dependencies[PACKAGES][index] = PYTHON_PREFIX + '=' + version
    421             else:
    422                 self._conda_dependencies[PACKAGES].append(PYTHON_PREFIX + '=' + version)

如果是这种情况,那么您需要以完全相同的顺序进行这些导入:

-D<some-property>.fileSet=['path-to-fileset']

之后,您应该可以执行以下操作:

"dependencies": {
  "@types/moment-duration-format": "2.2.2",
  "moment": "2.24.0",
  "moment-duration-format": "2.3.2"
}