typescript - 云函数中的 Nanoid Typescript - 错误 : typeof import has no call signatures

标签 typescript api google-cloud-functions

导入

import * as customAlphabet from "nanoid";
var id: string = ""
const alphabet: string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

function generateID() {
      id = customAlphabet(alphabet,10)
      console.log(id)
}

错误

id = customAlphabet(alphabet,10)

This expression is not callable.
  Type 'typeof import("/Users/../API/functions/node_modules/nanoid/index")' has no call signatures.

最佳答案

您需要在 imported module 上调用 customAlphabet 。这为您提供了一个生成器函数。您可以调用此生成器使用自定义字母集和自定义长度生成随机字符串。

import * as nanoid from "nanoid";

// Your alphabet set
const alphabet = '0123456789ABCD';

// generator is a function that returns a random string
// of length 10, with alphabets from the characters in `alphabet` constant 
const generator = nanoid.customAlphabet(alphabet, 10);

// some random string
console.log(generator());
// another random string
console.log(generator());

Codesandbox link

关于typescript - 云函数中的 Nanoid Typescript - 错误 : typeof import has no call signatures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66003439/

相关文章:

angular - Angular 错误处理和日志记录-调用GlobalErrorHandler的handleError

javascript - Webpack 极慢构建

javascript - 共享接口(interface)公共(public)功能的有效方法

python - 使用 GitHub API 检查用户是否存在

typescript 接口(interface)变量空检查并设置默认值

javascript - Firebase - Cloud Firestore 触发器幂等性

reactjs - 如何在 Filter<T> 作为参数传入的方法中从 Typescript 中的 Filter<T> 访问 $and 运算符?

Django密码重置确认

python - 使用 oauth 的 Twitter 身份验证问题?

typescript - node_modules/firebase-functions/lib/function-configuration.d.ts :4:64 - error TS1005: ']' expected