Búsqueda de sitios web

api - Nanoid Typescript en una función en la nube - Error: typeof import no tiene firmas de llamada


Importando

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

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

Error

id = customAlphabet(alphabet,10)

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

Respuesta #1

Necesitas llamar a customAlphabet en el módulo importado. Esto te da una función generadora. Puedes llamar a este generador para generar tu cadena aleatoria usando tu conjunto de alfabeto personalizado y longitud personalizada.

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());

Enlace de Codesandbox