TypeScript找不到名称

时间:2016-02-12 05:00:34

标签: mongoose typescript definitelytyped

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).

错误

  

找不到名字&#39; Foo&#39;

正如您所看到的,我甚至包括reference pathtsconfig.json,但无济于事。如何导入它?

1 个答案:

答案 0 :(得分:1)

避免将reference标记用于外部模块。

只需删除引用标记,然后在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();
foo.ts中的

或外部模块foo

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

并将其导入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
    }));