如何从Typescript中的另一个文件声明一个函数? [英] How can I declare a function from another file in Typescript?

查看:840
本文介绍了如何从Typescript中的另一个文件声明一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有以下功能:

I have the following function in a file:

function alertWin(title, message) {
   .......
   .......
}

在另一个打字稿文件中我有:

In another typescript file I have:

function mvcOnFailure(message) {
    "use strict";
    alertWin("Internal Application Error", message);
}

我收到一条错误,说当前范围内不存在alertwin 。

I am getting an error saying "alertwin" does not exist in the current scope.

解决这个问题的方法是让我在另一个文件中定义这个函数然后引用它吗?如果是,那么定义会是什么样的?

Is the way to solve this for me to define this function in another file and then reference that? If so then what would the definition look like?

推荐答案

你可以这样做(假设标题和消息都应该是字符串):

You can do this (assuming title and message are both supposed to be strings):

interface alertWinInterface{
    (title:string, message: string):any;
}

declare var alertWin: alertWinInterface;

您可以将其放在同一个文件中,或者将其放在单独的环境定义文件中(.d .ts)你导入的:

You could put this in the same file, or put it in a separate ambient definitions file (.d.ts) which you import:

/// <reference path="myDefinitions.d.ts" />

或者,您可以导入具有实际功能定义的其他文件,但您不会获得静态类型支持。

Or, you could just import the other file that has the actual function definition, but you won't get static typing support.

这篇关于如何从Typescript中的另一个文件声明一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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