0

models.d.ts [mongoose.d.ts]

/// <reference path='./../../typings/mongoose/mongoose.d.ts' />

import * as mongoose from 'mongoose';

// Tried `V0`, `V1`, `V2`, `V3` independently. None worked.

/* V0 */
export interface Foo extends mongoose.Document {
    name: string;
}

models.ts

/// <reference path='./models.d.ts' />

import * as mongoose from 'mongoose';

export function Foo(model): Foo.Foo /* also tried `Foo` and `models.Foo` */ {
    return mongoose.model('Foo', new mongoose.Schema({
        name: String
    }));
}

More context + attempts (gist).

Error

Cannot find name 'Foo'

As you can see, I even included reference path AND tsconfig.json, to no avail. How do I import it?

1 Answer 1

1

Avoid using reference tag with external module.

Simply remove reference tag, and write all in models.ts

import * as mongoose from 'mongoose';

export interface IFoo extends mongoose.Document {
    name: string;
}

export var Foo: mongoose.Model<IFoo> =
    mongoose.model<IFoo>('Foo', new mongoose.Schema({
        name: String
    }));

var foo0: IFoo = new Foo();

or external module foo in foo.ts

export interface Foo extends mongoose.Document {
    name: string;
}

and import it in models.ts

import * as mongoose from 'mongoose';
import * as foo from './foo';

export const Foo: mongoose.Model<foo.Foo> =
    mongoose.model<foo.Foo>('Foo', new mongoose.Schema({
        name: String
    }));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't know that the syntax had moved away from reference and towards import. I thought that with tsconfig we didn't need reference, and everything was just visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.