guid/uuid in Typescript Node.js app

后端 未结 4 1347
挽巷
挽巷 2021-02-01 00:55

I try to make a uuid (v 3.0.1) package work in Node/Typescript app, but I\'m not sure what should I import and how to use it.

This is index.d.ts

相关标签:
4条回答
  • 2021-02-01 01:34
    import * as uuid from "uuid";
    const id: string = uuid.v4();
    
    0 讨论(0)
  • 2021-02-01 01:34

    Per the tests:

    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/uuid/uuid-tests.ts

    import uuid = require('uuid');
    
    let uuidv4: string = uuid.v4();
    
    0 讨论(0)
  • 2021-02-01 01:47

    Yes, here is code from my project:

    import { v4 as uuid } from 'uuid';
    const id: string = uuid();
    

    Note: to install definitions it is required to run

    npm install --save-dev @types/uuid
    
    0 讨论(0)
  • 2021-02-01 01:56

    This also works for me:

    import uuidv4 from 'uuid/v4'
    
    this.projectId = uuidv4()
    
    0 讨论(0)
提交回复
热议问题