首页 > 解决方案 > 在 Typescript 中,当我使用 require() 导入时,如何让它理解类型?

问题描述

我在 IntelliJ 中使用 TypeScript。

假设我有这个:

const functions = require('firebase-functions');

然后我像这样使用它:

exports.doSomething = functions.https.onCall((data, context) => {...}

但是,它实际上并没有链接https到任何东西。它被认为是 type any。然后它也不知道它的类型onCall

我发现我可以通过将其添加到文件顶部来解决此问题:

import {onCall} from "firebase-functions/lib/providers/https";

然后像这样使用它:

exports.doSomething = onCall((data, context) => {...}

但是,这不是一个好的解决方案,因为其他名称空间可能包含onCall,我需要区分它们。

所以,我的问题是:当我使用导入东西时,如何让它理解类型require()

标签: typescripttypescript-typings

解决方案


您需要使用以下import name = require ('path')语法:

 import functions = require('firebase-functions')

你也可以试试

import * as functions 'firebase-functions';

如果您使用适当的模块系统,导入将被转换为require


推荐阅读