typescript named parameters like used in angularjs?

后端 未结 1 917
花落未央
花落未央 2020-12-31 09:14

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

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

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 09:59

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

    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 could be passed in varying orders in plain JavaScript) or create a set of function overloads that matched the possibilities I wanted to expose, like this:

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题