Skip to content

Commit ad3f2ff

Browse files
author
Anatoly Ostrovsky
committed
Type improvements
1 parent 04fa7bb commit ad3f2ff

File tree

13 files changed

+97
-38
lines changed

13 files changed

+97
-38
lines changed

@types/core/di/inteface.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface StorageLike {
2+
getItem(key: string): string | null;
3+
setItem(key: string, value: string): void;
4+
removeItem(key: string): void;
5+
}
6+
export interface PersistentStoreConfig {
7+
backend?: StorageLike;
8+
serialize?: (value: unknown) => string;
9+
deserialize?: (value: string) => unknown;
10+
cookie?: Record<string, unknown>;
11+
}

@types/interface.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Attributes } from "./core/compile/attributes.js";
22
import { Scope } from "./core/scope/scope.js";
33
import { NgModelController } from "./directive/model/model.js";
4+
export interface Constructor<T = any> {
5+
new (...args: any[]): T;
6+
}
47
export declare const PublicInjectionTokens: {
58
readonly $angular: "$angular";
69
readonly $attrs: "$attrs";

@types/services/storage/storage.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
* Creates a proxy that automatically persists an object's state
33
* into a storage backend whenever a property is set.
44
*
5-
* @param {object} target - The object to wrap
5+
* @param {Record<PropertyKey, any>} target - The object to wrap
66
* @param {string} key - The storage key
7-
* @param {object} storage - Any storage-like object with getItem/setItem/removeItem
7+
* @param {import("../../core/di/inteface").StorageLike & import("../../core/di/inteface").PersistentStoreConfig} storage - Any storage-like object with getItem/setItem/removeItem
88
* @param {{serialize?: function, deserialize?: function}} [options] - Optional custom (de)serialization
9-
* @returns {Proxy}
9+
* @returns {Record<PropertyKey, any>}
1010
*/
1111
export function createPersistentProxy(
12-
target: object,
12+
target: Record<PropertyKey, any>,
1313
key: string,
14-
storage: object,
14+
storage: any & any,
1515
options?: {
1616
serialize?: Function;
1717
deserialize?: Function;
1818
},
19-
): ProxyConstructor;
19+
): Record<PropertyKey, any>;

@types/shared/cache.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

@types/shared/dom.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ export function animatedomInsert(element: any, parent: any, after: any): void;
258258
* @returns {string} The base href.
259259
*/
260260
export function getBaseHref(): string;
261+
/**
262+
* Expando cache for adding properties to DOM nodes with JavaScript.
263+
* This used to be an Object in JQLite decorator, but swapped out for a Map
264+
*
265+
* @type {Map<number, import('../interface.ts').ExpandoStore>}
266+
*/
267+
export const Cache: Map<number, import("../interface.ts").ExpandoStore>;
261268
/**
262269
* A list of boolean attributes in HTML.
263270
* @type {string[]}

src/core/compile/compile.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
getIsolateScope,
1111
} from "../../shared/dom.js";
1212
import { isFunction, getNodeName, extend, assert } from "../../shared/utils.js";
13-
import { Cache } from "../../shared/cache.js";
13+
import { Cache } from "../../shared/dom.js";
1414
import { wait } from "../../shared/test-utils.js";
1515

1616
function isUnknownElement(el) {

src/core/di/injector.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ export function createInjector(modulesToLoad, strictDi = false) {
3232
/** @type {Map<String|Function, boolean>} */
3333
const loadedModules = new Map(); // Keep track of loaded modules to avoid circular dependencies
3434

35+
/**
36+
* @typedef {{
37+
* $provide: {
38+
* provider: Function,
39+
* factory: Function,
40+
* service: Function,
41+
* value: Function,
42+
* constant: Function,
43+
* store: Function,
44+
* decorator: Function,
45+
* },
46+
* $injectorProvider?: {
47+
* $get: () => unknown
48+
* },
49+
* $injector?: ProviderInjector
50+
* }} ProviderCache
51+
*/
52+
53+
/** @type {ProviderCache} */
3554
const providerCache = {
3655
$provide: {
3756
provider: supportObject(provider),
@@ -188,12 +207,11 @@ export function createInjector(modulesToLoad, strictDi = false) {
188207
* Registers a service persisted in a storage
189208
*
190209
* @param {string} name - Service name
191-
* @param {Function} ctor - Constructor for the service
210+
* @param {import("../../interface.ts").Constructor} ctor - Constructor for the service
192211
* @param {ng.StorageType} type - Type of storage to be instantiated
193-
* @param {Storage|Object} backendOrConfig - Either a Storage-like object (getItem/setItem) or a config object
194-
* with { backend, serialize, deserialize }
212+
* @param {import("./inteface.ts").StorageLike & import("./inteface.ts").PersistentStoreConfig} [backendOrConfig]
195213
*/
196-
function store(name, ctor, type, backendOrConfig = {}) {
214+
function store(name, ctor, type, backendOrConfig) {
197215
return provider(name, {
198216
$get: /** @param {ng.InjectorService} $injector */ ($injector) => {
199217
switch (type) {
@@ -212,11 +230,11 @@ export function createInjector(modulesToLoad, strictDi = false) {
212230

213231
const $cookie = $injector.get($injectTokens._cookie);
214232

215-
const serialize = backendOrConfig.serialize ?? JSON.stringify;
233+
const serialize = backendOrConfig?.serialize ?? JSON.stringify;
216234

217-
const deserialize = backendOrConfig.deserialize ?? JSON.parse;
235+
const deserialize = backendOrConfig?.deserialize ?? JSON.parse;
218236

219-
const cookieOpts = backendOrConfig.cookie ?? {};
237+
const cookieOpts = backendOrConfig?.cookie ?? {};
220238

221239
return createPersistentProxy(instance, name, {
222240
getItem(key) {
@@ -266,10 +284,15 @@ export function createInjector(modulesToLoad, strictDi = false) {
266284
backend = localStorage;
267285
}
268286

269-
return createPersistentProxy(instance, name, backend, {
270-
serialize,
271-
deserialize,
272-
});
287+
return createPersistentProxy(
288+
instance,
289+
name,
290+
/** @type {import("./inteface.ts").StorageLike} */ (backend),
291+
{
292+
serialize,
293+
deserialize,
294+
},
295+
);
273296
}
274297
}
275298

src/core/di/inteface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface StorageLike {
2+
getItem(key: string): string | null;
3+
setItem(key: string, value: string): void;
4+
removeItem(key: string): void;
5+
}
6+
7+
export interface PersistentStoreConfig {
8+
backend?: StorageLike;
9+
serialize?: (value: unknown) => string;
10+
deserialize?: (value: string) => unknown;
11+
cookie?: Record<string, unknown>;
12+
}

src/interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { Attributes } from "./core/compile/attributes.js";
22
import { Scope } from "./core/scope/scope.js";
33
import { NgModelController } from "./directive/model/model.js";
44

5+
export interface Constructor<T = any> {
6+
new (...args: any[]): T;
7+
}
8+
59
export const PublicInjectionTokens = {
610
$angular: "$angular",
711
$attrs: "$attrs",

src/router/router.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ export class RouterProvider {
2727
/**
2828
* @type {Queue<import("./transition/transition.js").Transition>}
2929
*/
30-
this._transitionHistory = new Queue([], 1);
30+
this._transitionHistory = new Queue(
31+
/** @type {Array<import("./transition/transition.js").Transition>} */ ([]),
32+
1,
33+
);
3134

3235
/**
3336
* @type {Queue<import("./transition/transition.js").Transition>}
3437
*/
35-
this._successfulTransitions = new Queue([], 1);
38+
this._successfulTransitions = new Queue(
39+
/** @type {Array<import("./transition/transition.js").Transition>} */ ([]),
40+
1,
41+
);
3642

3743
/**
3844
* @type {import("./state/interface.ts").StateDeclaration|undefined}

0 commit comments

Comments
 (0)