typescript 打字稿命名参数就像在angularjs中使用的一样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14931824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:48:01  来源:igfitidea点击:

typescript named parameters like used in angularjs?

angularjstypescript

提问by JasonS

I'm trying to learn both typescript and angularjs (and thus javascript as i'm a noob)

我正在尝试同时学习 typescript 和 angularjs(以及 javascript,因为我是一个菜鸟)

going through the angularjs tutorials, I see they do stuff like this:

通过 angularjs 教程,我看到他们做这样的事情:

in normal javascript:

在普通的 javascript 中:

    //for full example, see the "Wire up a backend" project.js example 
//on the main page of http://angularjs.org 
            function CreateCtrl($scope, $location, Project){
        //do stuff
        }

the kicker is, those parameters could be in any order (or not there at all), and Project is actually a user defined variable/type. The angularjs framework is able to map the parameters to actual objects.

关键是,这些参数可以按任何顺序排列(或根本不存在),而 Project 实际上是用户定义的变量/类型。angularjs 框架能够将参数映射到实际对象。

So now to Typescript, how can I recreate this type of functionality? I actually would like to describe angularjs's behavior in some way to let me wrap it in typescript, (strongly type this flexible property injection)

那么现在到 Typescript,我怎样才能重新创建这种类型的功能?我实际上想以某种方式描述 angularjs 的行为,让我将它包装在打字稿中,(强烈键入这种灵活的属性注入)

any ideas?

有任何想法吗?

采纳答案by Fenton

There is an AngularJS type definition on Definitely Typedthat lets you use Angular from TypeScript.

在绝对类型上有一个AngularJS 类型定义,它允许您使用 TypeScript 中的 Angular。

If I was creating a definition for an existing function such as this (in the absence of named arguments), I would probably either define them in a specific order (even though I know they couldbe passed in varying orders in plain JavaScript) or create a set of function overloads that matched the possibilities I wanted to expose, like this:

如果我为这样的现有函数创建一个定义(在没有命名参数的情况下),我可能会以特定的顺序定义它们(即使我知道它们可以在纯 JavaScript 中以不同的顺序传递)或创建一组与我想公开的可能性相匹配的函数重载,如下所示:

interface Example {
    ($scope: bool, $location: string, Project: number): void;
    ($location: string, $scope: bool, Project: number): void;
    (Project: number, $scope: bool, $location: string): void;
}

declare var example: Example;

When I attempt to call example(I get intellisense with the three options and if I don't use one of these combinations I get a compiler warning.

当我尝试调用时,example(我会得到三个选项的智能感知,如果我不使用这些组合之一,我会收到编译器警告。

In JavaScript, named arguments are normally created with an object, so if I was writing a new method that could accept arguments in this way I would do this...

在 JavaScript 中,命名参数通常是用一个对象创建的,所以如果我正在编写一个可以以这种方式接受参数的新方法,我会这样做......

interface ExampleArguments {
    scope: bool;
    location: string;
    project: number;
}

var example = function (exampleArguments: ExampleArguments) {

};

example({ scope: true, project: 4, location: 'hi' });

Which is statically typed and checked in TypeScript.

这是静态类型并在 TypeScript 中检查的。