2
  1. The dynamoose library provides the following signature for constructing a model:
    model: {
        <T extends Document>(name: string, schema?: SchemaDefinition | Schema, options?: Partial<import("./Model").ModelOptions>): T & Model<T> & ModelDocumentConstructor<T>;
        defaults: any;
    };
  1. Consumption in a TypeScript file is being done as follows:
import * as dynamoose from 'dynamoose';
...
const ExampleRepository = dynamoose.model(
  schemaName,
  ExampleSchema,
);

export default ExampleRepository;
  1. Compilation produces the error:
Exported variable 'ExampleRepository' has or is using name 'ModelDocumentConstructor' from external module ".../node_modules/dynamoose/dist/index" but cannot be named.

Other answers to similar questions on SO would suggest that the ModelDocumentConstructor should be imported. TS4023: Exported Variable <x> has or is using name <y> from external module but cannot be named

However the interface itself is not exported, so cannot be imported.

beatyt
  • 98
  • 1
  • 9

3 Answers3

2

Since ModelDocumentConstructor is not exported, I have implemented the following solution as a workaround. Create a second wrapper object to export, which contains the methods you need.

For example:

const Model = dynamoose.model('MyObj', Schema, {
  create: false,
  update: false,
});

async function add(obj: {}) {  
  try {
    await Model.create(obj);
  } catch (e) {
    console.error(e);
  }
}

async function findMany(obj: { [key: string]: any }) {  
  try {
    return await Model.batchGet(obj);
  } catch (e) {
    console.error(e);
  } 
}

...

export const myObjDb = {
  find,
  findMany,
  add,
  remove,
  update,
  updateMany,
};
Ben Biz
  • 46
  • 3
0

From document, it says typescript support is in beta, but npm install --save "dynamoose@beta" does not work.

Is the latest release support typescript already, or very limted.

Nick Yang
  • 71
  • 2
0

This problem is showing up if "declaration": true option is set in tsconfig.json file. Disabling it if it's not required for the project will resolve the problem.

I've filled in a bug report in dynamoose repository so hopefully it will be fixed in the next version.