chore: improve angular2.d.ts file

- support ambient and import format for .d.ts
This commit is contained in:
Misko Hevery 2015-07-02 22:26:07 +02:00 committed by Alex Eagle
parent 65a767d9b0
commit 0052c6b120
4 changed files with 50 additions and 25 deletions

View File

@ -9,11 +9,14 @@
interface List<T> extends Array<T> {} interface List<T> extends Array<T> {}
interface Map<K,V> {} interface Map<K,V> {}
interface StringMap<K,V> extends Map<K,V> {} interface StringMap<K,V> extends Map<K,V> {}
interface Type {} declare type Type = ng.Type;
declare module "angular2/angular2" { declare module ng {
type SetterFn = typeof Function; type SetterFn = typeof Function;
type int = number; type int = number;
interface Type extends Function {
new (...args);
}
// See https://github.com/Microsoft/TypeScript/issues/1168 // See https://github.com/Microsoft/TypeScript/issues/1168
class BaseException /* extends Error */ { class BaseException /* extends Error */ {
@ -22,4 +25,8 @@ declare module "angular2/angular2" {
toString(): string; toString(): string;
} }
} }
{% endblock %} {% endblock %}
declare module "angular2/angular2" {
export = ng;
}

View File

@ -32,7 +32,7 @@
{% block staticDeclarations %}{% endblock %} {% block staticDeclarations %}{% endblock %}
{% for alias, module in doc.moduleDocs %} {% for alias, module in doc.moduleDocs %}
{$ commentBlock(module.doc, 1) $} {$ commentBlock(module.doc, 1) $}
declare module "{$ module.id $}" { declare module ng {
{%- for export in module.doc.exports -%} {%- for export in module.doc.exports -%}
{%- if export.content -%} {%- if export.content -%}
@ -68,3 +68,6 @@ declare module "{$ module.id $}" {
{% endfor %} {% endfor %}
declare module "angular2/angular2" {
export = ng;
}

View File

@ -6,43 +6,56 @@ import {
} from './annotations'; } from './annotations';
import {ViewAnnotation, ViewArgs} from './view'; import {ViewAnnotation, ViewArgs} from './view';
import {AttributeAnnotation, QueryAnnotation} from './di'; import {AttributeAnnotation, QueryAnnotation} from './di';
import {makeDecorator, makeParamDecorator, TypeDecorator, Class} from '../../util/decorators'; import {
makeDecorator,
makeParamDecorator,
TypeDecorator,
ParamaterDecorator,
Class
} from '../../util/decorators';
import {Type} from 'angular2/src/facade/lang'; import {Type} from 'angular2/src/facade/lang';
export {ClassDefinition, TypeDecorator} from '../../util/decorators'; export interface DirectiveDecorator extends TypeDecorator {}
export interface DirectiveTypeDecorator extends TypeDecorator {} export interface ComponentDecorator extends TypeDecorator { View(obj: ViewArgs): ViewDecorator; }
export interface ComponentTypeDecorator extends TypeDecorator { export interface ViewDecorator extends TypeDecorator { View(obj: ViewArgs): ViewDecorator }
View(obj: ViewArgs): ViewTypeDecorator;
}
export interface ViewTypeDecorator extends TypeDecorator { View(obj: ViewArgs): ViewTypeDecorator } export interface DirectiveFactory {
(obj: DirectiveArgs): DirectiveDecorator;
export interface IDirective {
(obj: DirectiveArgs): DirectiveTypeDecorator;
new (obj: DirectiveAnnotation): DirectiveAnnotation; new (obj: DirectiveAnnotation): DirectiveAnnotation;
} }
export interface IComponent { export interface ComponentFactory {
(obj: ComponentArgs): ComponentTypeDecorator; (obj: ComponentArgs): ComponentDecorator;
new (obj: ComponentAnnotation): ComponentAnnotation; new (obj: ComponentAnnotation): ComponentAnnotation;
} }
export interface IView { export interface ViewFactory {
(obj: ViewArgs): ViewTypeDecorator; (obj: ViewArgs): ViewDecorator;
new (obj: ViewArgs): ViewAnnotation; new (obj: ViewArgs): ViewAnnotation;
} }
export interface AttributeFactory {
(name: string): TypeDecorator;
new (name: string): AttributeAnnotation;
}
export interface QueryFactory {
(selector: Type | string, {descendants}?: {descendants?: boolean}): ParameterDecorator;
new (selector: Type | string, {descendants}?: {descendants?: boolean}): QueryAnnotation;
}
/* from annotations */ /* from annotations */
export var Component: IComponent = export var Component: ComponentFactory =
<IComponent>makeDecorator(ComponentAnnotation, (fn: any) => fn.View = View); <ComponentFactory>makeDecorator(ComponentAnnotation, (fn: any) => fn.View = View);
export var Directive: IDirective = <IDirective>makeDecorator(DirectiveAnnotation); export var Directive: DirectiveFactory = <DirectiveFactory>makeDecorator(DirectiveAnnotation);
/* from view */ /* from view */
export var View: IView = <IView>makeDecorator(ViewAnnotation, (fn: any) => fn.View = View); export var View: ViewFactory =
<ViewFactory>makeDecorator(ViewAnnotation, (fn: any) => fn.View = View);
/* from di */ /* from di */
export var Attribute = makeParamDecorator(AttributeAnnotation); export var Attribute: AttributeFactory = makeParamDecorator(AttributeAnnotation);
export var Query = makeParamDecorator(QueryAnnotation); export var Query: QueryFactory = makeParamDecorator(QueryAnnotation);

View File

@ -6,11 +6,13 @@ export interface ClassDefinition {
} }
export interface TypeDecorator { export interface TypeDecorator {
(cls: any): any; <T>(cls: T): T;
annotations: Array<any>; annotations: Array<any>;
Class(obj: ClassDefinition): Type; Class(obj: ClassDefinition): Type;
} }
export interface ParamaterDecorator { (cls: Type, unusedKey: any, index: number): void }
function extractAnnotation(annotation: any): any { function extractAnnotation(annotation: any): any {
if (isFunction(annotation) && annotation.hasOwnProperty('annotation')) { if (isFunction(annotation) && annotation.hasOwnProperty('annotation')) {
// it is a decorator, extract annotation // it is a decorator, extract annotation