Skip to content

Commit 423cfcf

Browse files
dgp1130clydin
authored andcommitted
refactor(@angular-devkit/core): fix strict types in experimental.
Most of the problems here come from return-only generics. A greater redesign of these core types is necessary to properly fix all the typing issues. However, all these types seem to be exported publicly. In the interest of not introducing any breaking changes, most of these fixes were just casting types to `unknown` or replacing generics with the maximally-allowed type (`JsonValue`).
1 parent 5dc60f1 commit 423cfcf

File tree

6 files changed

+46
-33
lines changed

6 files changed

+46
-33
lines changed

packages/angular_devkit/core/src/experimental/jobs/dispatcher.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,29 @@ export function createDispatcher<
5353
options: Partial<Readwrite<JobDescription>> = {},
5454
): JobDispatcher<A, I, O> {
5555
let defaultDelegate: JobName | null = null;
56-
const conditionalDelegateList: [(args: A) => boolean, JobName][] = [];
56+
const conditionalDelegateList: [(args: JsonValue) => boolean, JobName][] = [];
5757

58-
const job: JobHandler<A, I, O> = Object.assign((argument: A, context: JobHandlerContext) => {
59-
const maybeDelegate = conditionalDelegateList.find(([predicate]) => predicate(argument));
60-
let delegate: Job<A, I, O> | null = null;
58+
const job: JobHandler<JsonValue, JsonValue, JsonValue> = Object.assign(
59+
(argument: JsonValue, context: JobHandlerContext) => {
60+
const maybeDelegate = conditionalDelegateList.find(([predicate]) => predicate(argument));
61+
let delegate: Job<JsonValue, JsonValue, JsonValue> | null = null;
6162

62-
if (maybeDelegate) {
63-
delegate = context.scheduler.schedule(maybeDelegate[1], argument);
64-
} else if (defaultDelegate) {
65-
delegate = context.scheduler.schedule(defaultDelegate, argument);
66-
} else {
67-
throw new JobDoesNotExistException('<null>');
68-
}
63+
if (maybeDelegate) {
64+
delegate = context.scheduler.schedule(maybeDelegate[1], argument);
65+
} else if (defaultDelegate) {
66+
delegate = context.scheduler.schedule(defaultDelegate, argument);
67+
} else {
68+
throw new JobDoesNotExistException('<null>');
69+
}
6970

70-
context.inboundBus.subscribe(delegate.inboundBus);
71+
context.inboundBus.subscribe(delegate.inboundBus);
7172

72-
return delegate.outboundBus;
73-
}, {
74-
jobDescription: options,
75-
});
73+
return delegate.outboundBus;
74+
},
75+
{
76+
jobDescription: options,
77+
},
78+
);
7679

7780
return Object.assign(job, {
7881
setDefaultJob(name: JobName | null | JobHandler<JsonValue, JsonValue, JsonValue>) {
@@ -82,8 +85,9 @@ export function createDispatcher<
8285

8386
defaultDelegate = name;
8487
},
85-
addConditionalJob(predicate: (args: A) => boolean, name: JobName) {
88+
addConditionalJob(predicate: (args: JsonValue) => boolean, name: JobName) {
8689
conditionalDelegateList.push([predicate, name]);
8790
},
88-
});
91+
// TODO: Remove return-only generic from createDispatcher() API.
92+
}) as unknown as JobDispatcher<A, I, O>;
8993
}

packages/angular_devkit/core/src/experimental/jobs/dispatcher_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { JsonValue } from '../../json';
9+
import { JobHandler } from './api';
810
import { createJobHandler } from './create-job-handler';
911
import { createDispatcher } from './dispatcher';
1012
import { SimpleJobRegistry } from './simple-registry';
@@ -31,7 +33,7 @@ describe('createDispatcher', () => {
3133
registry.register(add0);
3234
registry.register(add100);
3335

34-
dispatcher.setDefaultJob(add0);
36+
dispatcher.setDefaultJob(add0 as JobHandler<JsonValue, JsonValue, number>);
3537
const sum = scheduler.schedule('add', [1, 2, 3, 4]);
3638
expect(await sum.output.toPromise()).toBe(10);
3739
});

packages/angular_devkit/core/src/experimental/jobs/simple-registry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SimpleJobRegistry<
3434
I extends MinimumInputValueT = MinimumInputValueT,
3535
O extends MinimumOutputValueT = MinimumOutputValueT,
3636
>(name: JobName): Observable<JobHandler<A, I, O> | null> {
37-
return of(this._jobNames.get(name) as (JobHandler<A, I, O> | null) || null);
37+
return of(this._jobNames.get(name) as unknown as (JobHandler<A, I, O> | null) || null);
3838
}
3939

4040
/**
@@ -132,7 +132,9 @@ export class SimpleJobRegistry<
132132
input,
133133
};
134134

135-
this._jobNames.set(name, Object.assign(handler.bind(undefined), { jobDescription }));
135+
const jobHandler = Object.assign(handler.bind(undefined), { jobDescription }) as
136+
unknown as JobHandler<MinimumArgumentValueT, MinimumInputValueT, MinimumOutputValueT>;
137+
this._jobNames.set(name, jobHandler);
136138
}
137139

138140
/**

packages/angular_devkit/core/src/experimental/jobs/simple-scheduler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class SimpleScheduler<
162162
argumentV: this._schemaRegistry.compile(description.argument).pipe(shareReplay(1)),
163163
inputV: this._schemaRegistry.compile(description.input).pipe(shareReplay(1)),
164164
outputV: this._schemaRegistry.compile(description.output).pipe(shareReplay(1)),
165-
});
165+
}) as JobHandlerWithExtra;
166166
this._internalJobDescriptionMap.set(name, handlerWithExtra);
167167

168168
return of(handlerWithExtra);
@@ -419,7 +419,7 @@ export class SimpleScheduler<
419419

420420
const output = outboundBus.pipe(
421421
filter(x => x.kind == JobOutboundMessageKind.Output),
422-
map((x: JobOutboundMessageOutput<O>) => x.value),
422+
map((x) => (x as JobOutboundMessageOutput<O>).value),
423423
shareReplay(1),
424424
);
425425

@@ -444,7 +444,7 @@ export class SimpleScheduler<
444444
let maybeObservable = channels.get(name);
445445
if (!maybeObservable) {
446446
const s = new Subject<T>();
447-
channelsSubject.set(name, s);
447+
channelsSubject.set(name, s as unknown as Subject<JsonValue>);
448448
channels.set(name, s.asObservable());
449449

450450
maybeObservable = s.asObservable();
@@ -535,7 +535,7 @@ export class SimpleScheduler<
535535

536536
return handler(argument, context);
537537
}),
538-
).subscribe(subscriber);
538+
).subscribe(subscriber as Observer<JobOutboundMessage<JsonValue>>);
539539
})),
540540
),
541541
);

packages/angular_devkit/core/src/experimental/jobs/strategy_spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('strategy.serialize()', () => {
3333
resolve(input.reduce((a, c) => a + c, 0));
3434
}, 10),
3535
);
36-
})), {
36+
// tslint:disable-next-line:no-any
37+
}) as any), {
3738
argument: { items: { type: 'number' } },
3839
output: { type: 'number' },
3940
name: 'add',
@@ -82,7 +83,8 @@ describe('strategy.serialize()', () => {
8283
resolve(input.reduce((a, c) => a + c, 0));
8384
}, 10),
8485
);
85-
})), {
86+
// tslint:disable-next-line:no-any
87+
}) as any), {
8688
argument: { items: { type: 'number' } },
8789
output: { type: 'number' },
8890
name: 'add',
@@ -96,7 +98,8 @@ describe('strategy.serialize()', () => {
9698
resolve(input.reduce((a, c) => a + c, 100));
9799
}, 10),
98100
);
99-
})), {
101+
// tslint:disable-next-line:no-any
102+
}) as any), {
100103
argument: { items: { type: 'number' } },
101104
output: { type: 'number' },
102105
name: 'add100',
@@ -153,7 +156,8 @@ describe('strategy.reuse()', () => {
153156
resolve(input.reduce((a, c) => a + c, 0));
154157
}, 10),
155158
);
156-
})), {
159+
// tslint:disable-next-line:no-any
160+
}) as any), {
157161
argument: { items: { type: 'number' } },
158162
output: { type: 'number' },
159163
name: 'add',
@@ -220,7 +224,8 @@ describe('strategy.memoize()', () => {
220224
resolve(input.reduce((a, c) => a + c, 0));
221225
}, 10),
222226
);
223-
})), {
227+
// tslint:disable-next-line:no-any
228+
}) as any), {
224229
argument: { items: { type: 'number' } },
225230
output: { type: 'number' },
226231
name: 'add',

packages/angular_devkit/core/src/experimental/workspace/workspace.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export class Workspace {
9090
];
9191

9292
private readonly _workspaceSchemaPath = normalize(require.resolve('./workspace-schema.json'));
93-
private _workspaceSchema: JsonObject;
94-
private _workspace: WorkspaceSchema;
93+
private _workspaceSchema?: JsonObject;
94+
private _workspace!: WorkspaceSchema;
9595
private _registry: schema.CoreSchemaRegistry;
9696

9797
constructor(
@@ -129,7 +129,7 @@ export class Workspace {
129129

130130
loadWorkspaceFromJson(json: {}) {
131131
return this._loadWorkspaceSchema().pipe(
132-
concatMap((workspaceSchema) => this.validateAgainstSchema(json, workspaceSchema)),
132+
concatMap((workspaceSchema) => this.validateAgainstSchema<WorkspaceSchema>(json, workspaceSchema)),
133133
tap((validatedWorkspace: WorkspaceSchema) => this._workspace = validatedWorkspace),
134134
map(() => this),
135135
);

0 commit comments

Comments
 (0)