小编Eri*_*rth的帖子

如何在打字稿中使用静态方法扩展和输入导入的类

我正在尝试使用静态方法扩展 Luxon DateTime 类,如下所示:

DateTime.fromDob = function(dob: number | string | DateTime) {
  if (dob instanceof DateTime) {
    return dob
  }
  if (dob && (typeof dob === 'number' || typeof dob === 'string')) {
    return DateTime.fromFormat(String(dob), DOB)
  }
  return null
}
Run Code Online (Sandbox Code Playgroud)

打字稿错误:Property 'isInFuture' does not exist on type 'typeof DateTime'.ts(2339)

我尝试将该方法添加到 d.ts 文件中的模块声明中,但无济于事:

declare module 'luxon/src/datetime' {
  export interface DateTime  {
    public static fromDob(dob: number | string | DateTime): DateTime | null
  }
}
Run Code Online (Sandbox Code Playgroud)

但这并不能解决错误。

class typescript luxon

1
推荐指数
1
解决办法
79
查看次数

标签 统计

class ×1

luxon ×1

typescript ×1