导入时打字稿类型保护丢失类型定义

时间:2020-12-21 21:04:21

标签: typescript

好吧,我遇到了一个奇怪的情况,我在一个文件中有一个自定义的打字稿类型保护:

import _ from 'lodash'
import { JSONSchemaWrapper } from 'types/JSONSchema'

export const isJsonSchema = (jsonSchema: any): jsonSchema is JSONSchemaWrapper => {
  const _jsonSchema = jsonSchema as JSONSchemaWrapper
  return _.isPlainObject(_jsonSchema.properties)
}

当我导入它时,它在 : jsonSchema is any 处被拉出,使得类型检查变得毫无意义。

奇怪的是,如果我将代码复制并粘贴到我正在使用它的文件中,一切正常。似乎是丢失了 is JSONSchemaWrapper 并变成了 is any 看截图:

当在另一个文件中打字时: enter image description here

当 typeguard 代码直接粘贴到这个文件中时:

enter image description here

我也在“打字稿”上:“^3.9.5”

0 个答案:

没有答案