如何导入其他 TypeScript 文件? [英] How do I import other TypeScript files?

查看:73
本文介绍了如何导入其他 TypeScript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vs.net 使用 TypeScript 插件时,如何让一个 TypeScript 文件导入其他 TypeScript 文件中声明的模块?

When using the TypeScript plugin for vs.net, how do I make one TypeScript file import modules declared in other TypeScript files?

文件 1:

module moo
{
    export class foo .....
}

文件 2:

//what goes here?

class bar extends moo.foo
{
}

推荐答案

从 TypeScript 1.8 版开始,您可以使用简单的 import 语句,就像在 ES6 中一样:

From TypeScript version 1.8 you can use simple import statements just like in ES6:

import { ZipCodeValidator } from "./ZipCodeValidator";

let myValidator = new ZipCodeValidator();

https://www.typescriptlang.org/docs/handbook/modules.html

旧答案:从 TypeScript 1.5 版开始,您可以使用 tsconfig.json:http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Old answer: From TypeScript version 1.5 you can use tsconfig.json: http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

它完全消除了评论样式引用的需要.

It completely eliminates the need of the comment style referencing.

旧答案:

您需要引用当前文件顶部的文件.

You need to reference the file on the top of the current file.

你可以这样做:

/// <reference path="../typings/jquery.d.ts"/>
/// <reference path="components/someclass.ts"/>

class Foo { }

这些路径是相对于当前文件的.

These paths are relative to the current file.

你的例子:

/// <reference path="moo.ts"/>

class bar extends moo.foo
{
}

这篇关于如何导入其他 TypeScript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆