Skip to content

Commit 061eb5c

Browse files
committed
Type improvements
1 parent 23ce3f4 commit 061eb5c

File tree

21 files changed

+172
-69
lines changed

21 files changed

+172
-69
lines changed

@types/angular.d.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export class Angular {
33
private _bootsrappedModules;
44
/** @public @type {ng.PubSubService} */
55
public $eventBus: ng.PubSubService;
6+
/** @public @type {ng.InjectorService} */
7+
public $injector: ng.InjectorService;
68
/**
79
* @public
810
* @type {string} `version` from `package.json`
@@ -129,16 +131,12 @@ export class Angular {
129131
config?: import("./interface.ts").AngularBootstrapConfig,
130132
): ng.InjectorService;
131133
$rootScope: ng.Scope;
132-
$injector: import("./core/di/internal-injector.js").InjectorService;
133134
/**
134135
* @param {any[]} modules
135-
* @param {boolean?} strictDi
136-
* @returns {import("./core/di/internal-injector.js").InjectorService}
136+
* @param {boolean} [strictDi]
137+
* @returns {ng.InjectorService}
137138
*/
138-
injector(
139-
modules: any[],
140-
strictDi: boolean | null,
141-
): import("./core/di/internal-injector.js").InjectorService;
139+
injector(modules: any[], strictDi?: boolean): ng.InjectorService;
142140
/**
143141
* @param {Element|Document} element
144142
*/
@@ -152,9 +150,9 @@ export class Angular {
152150
* or defined on `$scope` injectable.
153151
*
154152
* @param {string} name
155-
* @returns {ProxyHandler<ng.Scope>|undefined}
153+
* @returns {Proxy<ng.Scope>|undefined}
156154
*/
157-
getScopeByName(name: string): ProxyHandler<ng.Scope> | undefined;
155+
getScopeByName(name: string): ProxyConstructor | undefined;
158156
}
159157
export type ModuleRegistry = {
160158
[x: string]: NgModule;

@types/core/compile/attributes.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ export class Attributes {
5959
* Set a normalized attribute on the element in a way such that all directives
6060
* can share the attribute. This function properly handles boolean attributes.
6161
* @param {string} key Normalized key. (ie ngAttribute)
62-
* @param {string|boolean} value The value to set. If `null` attribute will be deleted.
62+
* @param {string|boolean|null} value The value to set. If `null` attribute will be deleted.
6363
* @param {boolean=} writeAttr If false, does not write the value to DOM element attribute.
6464
* Defaults to true.
6565
* @param {string=} attrName Optional none normalized name. Defaults to key.
6666
*/
6767
$set(
6868
key: string,
69-
value: string | boolean,
69+
value: string | boolean | null,
7070
writeAttr?: boolean | undefined,
7171
attrName?: string | undefined,
7272
): void;

@types/core/scope/scope.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function createScope(target?: any, context?: Scope): Scope;
1616
* @returns {boolean}
1717
*/
1818
export function isNonScope(target: any): boolean;
19+
/** @ignore @type {Function[]}*/
20+
export const $postUpdateQueue: Function[];
1921
export class RootScopeProvider {
2022
rootScope: Scope;
2123
$get: (
@@ -176,6 +178,10 @@ export class Scope {
176178
* @returns {Scope|undefined}
177179
*/
178180
$getById(id: string | number): Scope | undefined;
179-
$searchByName(name: any): any;
181+
/**
182+
* @param {string} name
183+
* @returns {ng.Scope|undefined}
184+
*/
185+
$searchByName(name: string): ng.Scope | undefined;
180186
#private;
181187
}

@types/interface.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export interface DirectivePrePost {
378378
export type DirectiveLinkFn<T> = (
379379
scope: Scope,
380380
element: HTMLElement,
381-
attrs: Attributes & Record<string, any>,
381+
attrs: ng.Attributes,
382382
controller?: TController<T>,
383383
transclude?: (...args: any[]) => any,
384384
) => void;

@types/namespace.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ declare global {
115115
}
116116
export namespace ng {
117117
type Angular = TAngular;
118-
type Attributes = TAttributes & Record<string, any>;
118+
type Attributes = TAttributes & Record<string, string>;
119119
type Directive<TController = any> = TDirective<TController>;
120120
type DirectiveFactory = TDirectiveFactory;
121121
type AnnotatedDirectiveFactory = TAnnotatedDirectiveFactory;

@types/router/state/interface.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export interface StateDeclaration {
352352
* Note: [State] objects require unique names.
353353
* The name is used like an id.
354354
*/
355-
name?: string;
355+
name: string;
356356
/**
357357
* Abstract state indicator
358358
*
@@ -807,7 +807,7 @@ export interface StateDeclaration {
807807
* Represents a fully built StateObject, after registration in the StateRegistry
808808
* and application of all StateBuilder decorators.
809809
*/
810-
export interface BuiltStateDeclaration extends StateDeclaration {
810+
export type BuiltStateDeclaration = StateDeclaration & {
811811
/** Reference to the original StateDeclaration */
812812
self: StateDeclaration;
813813
/** Array of Resolvables built from the resolve / resolvePolicy */
@@ -826,7 +826,7 @@ export interface BuiltStateDeclaration extends StateDeclaration {
826826
parent?: BuiltStateDeclaration | null;
827827
/** Optional inherited data */
828828
data?: any;
829-
}
829+
};
830830
/**
831831
* The return type of a [[StateDeclaration.lazyLoad]] function
832832
*

@types/shared/strings.d.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@ export function maxLength(max: number, str: string): string;
1515
* If the string is already longer than the desired length, return the string.
1616
* Else returns the string, with extra spaces on the end, such that it reaches `length` characters.
1717
*
18-
* @param length the desired length of the string to return
19-
* @param str the input string
18+
* @param {number} length the desired length of the string to return
19+
* @param {string} str the input string
20+
*/
21+
export function padString(length: number, str: string): string;
22+
/**
23+
* @param {string} camelCase
24+
* @returns {string}
25+
*/
26+
export function kebobString(camelCase: string): string;
27+
/**
28+
* @param {Function} fn
29+
* @returns {string}
30+
*/
31+
export function functionToString(fn: Function): string;
32+
/**
33+
* @param {[]|Function} fn
34+
* @returns {string}
2035
*/
21-
export function padString(length: any, str: any): any;
22-
export function kebobString(camelCase: any): any;
23-
export function functionToString(fn: any): any;
24-
export function fnToString(fn: any): any;
36+
export function fnToString(fn: [] | Function): string;
2537
export function stringify(value: any): any;
2638
/**
2739
* Splits on a delimiter, but returns the delimiters in the array

src/angular.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { unnestR } from "./shared/common.js";
1919
import { EventBus } from "./services/pubsub/pubsub.js";
2020
import { $injectTokens as $t } from "./injection-tokens.js";
2121
import { annotate } from "./core/di/di.js";
22+
import { validateIsString } from "./shared/validate.js";
2223

2324
const ngMinErr = minErr("ng");
2425

@@ -39,6 +40,9 @@ export class Angular {
3940
/** @public @type {ng.PubSubService} */
4041
this.$eventBus = EventBus;
4142

43+
/** @public @type {ng.InjectorService} */
44+
this.$injector;
45+
4246
/**
4347
* @public
4448
* @type {string} `version` from `package.json`
@@ -263,10 +267,15 @@ export class Angular {
263267
return x.$$state().resolvables;
264268
})
265269
.reduce(unnestR, [])
266-
.filter((x) => {
267-
return x.deps === "deferred";
268-
})
270+
.filter(
271+
/** @param {import("./router/resolve/resolvable.js").Resolvable} x */ (
272+
x,
273+
) => {
274+
return x.deps === "deferred";
275+
},
276+
)
269277
.forEach(
278+
/** @param {import("./router/resolve/resolvable.js").Resolvable} resolvable */
270279
(resolvable) =>
271280
(resolvable.deps = annotate(
272281
resolvable.resolveFn,
@@ -281,18 +290,20 @@ export class Angular {
281290

282291
/**
283292
* @param {any[]} modules
284-
* @param {boolean?} strictDi
285-
* @returns {import("./core/di/internal-injector.js").InjectorService}
293+
* @param {boolean} [strictDi]
294+
* @returns {ng.InjectorService}
286295
*/
287296
injector(modules, strictDi) {
288-
return createInjector(modules, strictDi);
297+
this.$injector = createInjector(modules, strictDi);
298+
299+
return this.$injector;
289300
}
290301

291302
/**
292303
* @param {Element|Document} element
293304
*/
294305
init(element) {
295-
/** @type {Element} */
306+
/** @type {Element|undefined} */
296307
let appElement;
297308

298309
let module;
@@ -339,10 +350,14 @@ export class Angular {
339350
* or defined on `$scope` injectable.
340351
*
341352
* @param {string} name
342-
* @returns {ProxyHandler<ng.Scope>|undefined}
353+
* @returns {Proxy<ng.Scope>|undefined}
343354
*/
344355
getScopeByName(name) {
345-
const scope = this.$rootScope.$searchByName(name);
356+
validateIsString(name, "name");
357+
/** @type {ng.RootScopeService} */
358+
const $rootScope = this.$injector.get("$rootScope");
359+
360+
const scope = $rootScope.$searchByName(name);
346361

347362
if (scope) {
348363
return scope.$proxy;

src/core/compile/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class Attributes {
140140
* Set a normalized attribute on the element in a way such that all directives
141141
* can share the attribute. This function properly handles boolean attributes.
142142
* @param {string} key Normalized key. (ie ngAttribute)
143-
* @param {string|boolean} value The value to set. If `null` attribute will be deleted.
143+
* @param {string|boolean|null} value The value to set. If `null` attribute will be deleted.
144144
* @param {boolean=} writeAttr If false, does not write the value to DOM element attribute.
145145
* Defaults to true.
146146
* @param {string=} attrName Optional none normalized name. Defaults to key.

src/core/scope/scope.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ let $parse;
3636
/**@type {ng.ExceptionHandlerService} */
3737
let $exceptionHandler;
3838

39-
const $postUpdateQueue = [];
39+
/** @ignore @type {Function[]}*/
40+
export const $postUpdateQueue = [];
4041

4142
export class RootScopeProvider {
4243
constructor() {
@@ -1406,8 +1407,17 @@ export class Scope {
14061407
}
14071408
}
14081409

1410+
/**
1411+
* @param {string} name
1412+
* @returns {ng.Scope|undefined}
1413+
*/
14091414
$searchByName(name) {
1410-
const getByName = (scope, nameParam) => {
1415+
/**
1416+
* @param {ng.Scope} scope
1417+
* @param {string} nameParam
1418+
* @returns {ng.Scope|undefined}
1419+
*/
1420+
function getByName(scope, nameParam) {
14111421
if (scope.$scopename === nameParam) {
14121422
return scope;
14131423
} else {
@@ -1424,7 +1434,7 @@ export class Scope {
14241434

14251435
return res;
14261436
}
1427-
};
1437+
}
14281438

14291439
return getByName(this.$root, name);
14301440
}

0 commit comments

Comments
 (0)