Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use node short id module with angular 6 in typescript

I tried to use the node module in an Angular 6 typescript.

first: npm i shortid

In TypeScript class:

import { shortid } from 'shortid';

let Uid = shortid.generate();

but i got an error "can not find function generate()";

how to use it correctly ?

like image 421
Umair Abid Avatar asked Jun 10 '26 13:06

Umair Abid


1 Answers

Instead of

import { shortid } from 'shortid';

do

import { generate } from 'shortid';

and execute the code with

let Uid = generate();

shortid doesn't come with TypeScript typings preinstalled, so you won't get information about what exactly you can import, arguments and return values within IDEs like Visual Studio Code.
You can fix this by installing typings for shortid - go to Microsoft's TypeSearch website, enter "shortid" and you'll be redirected to the npm package @types/shortid, which you can install with npm i @types/shortid. You can also check the DefinitelyTyped repository directly, where a lot of types for javascript packages are published.

After installing they typings, the IDE will show you information and offer auto completion for shortid similar to native typescript modules.

like image 170
CGundlach Avatar answered Jun 12 '26 11:06

CGundlach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!