Search code examples
javascriptnode.jstypescriptcommonjs

Nodejs require('this')(that) in Typescript


I'm using @awspilot/dynamodb in a node lambda function, but also using typescript.

A standard nodejs var x = require('x') becomes import * as x from 'x' in typescript no problem - but @awspilot/dynamodb needs the slightly more unusual:

var $db = new AWS.DynamoDB()
var awspilotDB = require('@awspilot/dynamodb')($db)

Is there a typescript-y way of rendering this as an import and still passing the extra parameter?

Thanks,

D


Solution

  • require('@awspilot/dynamodb') is returning a function. Therefore the following should work fine:

    import * as DynamodbFactory from '@awspilot/dynamodb'
    const awspilotDB = DynamodbFactory($db)
    

    You can, of course, choose whatever name you like for DynamodbFactory.


    Alternatively you can also use the require function with TypeScript as described here