Skip to content

Commit e20f1a8

Browse files
rix0rrrmrgrain
andauthored
refactor(cli): remove generic argument from notify<T>() (#217)
The `IIoHost` interface has the following method: ```ts interface IIoHost { notify<T>(msg: IoMessage<T>): Promise<void>; } ``` The generic parameter `T` is only used once, without bounds, for a singleton argument in input position. This means it is equivalent to the following: ```ts interface IIoHost { notify(msg: IoMessage<unknown>): Promise<void>; } ``` (Preferring `unknown` over `any` so that implementors are forced to test for the type of the `data` field, just like they would need to do with an argument of type `T`) In the words of the [Java generics tutorial](https://docs.oracle.com/javase/tutorial/extra/generics/methods.html): > Generic methods allow type parameters to be used to express > dependencies among the types of one or more arguments to a method and/or > its return type. If there isn't such a dependency, a generic method > should not be used. Or [similar advice for TypeScript](https://effectivetypescript.com/2020/08/12/generics-golden-rule/): > Type Parameters Should Appear Twice > > Type parameters are for relating the types of multiple values. If a > type parameter is only used once in the function signature, it's not > relating anything. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Co-authored-by: Momo Kornher <[email protected]>
1 parent db60595 commit e20f1a8

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/io-host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface IIoHost {
55
* Notifies the host of a message.
66
* The caller waits until the notification completes.
77
*/
8-
notify<T>(msg: IoMessage<T>): Promise<void>;
8+
notify(msg: IoMessage<unknown>): Promise<void>;
99

1010
/**
1111
* Notifies the host of a message that requires a response.

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/private/io-helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ActionLessRequest<T, U> = Omit<IoRequest<T, U>, 'action'>;
1313
* Wraps a client provided IoHost and provides additional features & services to toolkit internal classes.
1414
*/
1515
export interface IoHelper extends IIoHost {
16-
notify<T>(msg: ActionLessMessage<T>): Promise<void>;
16+
notify(msg: ActionLessMessage<unknown>): Promise<void>;
1717
requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U>;
1818
}
1919

packages/@aws-cdk/toolkit-lib/test/_helpers/test-io-host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class TestIoHost implements IIoHost {
1818
this.requestSpy = jest.fn();
1919
}
2020

21-
public async notify<T>(msg: IoMessage<T>): Promise<void> {
21+
public async notify(msg: IoMessage<unknown>): Promise<void> {
2222
if (isMessageRelevantForLevel(msg, this.level)) {
2323
this.notifySpy(msg);
2424
}

packages/aws-cdk/lib/cli/activity-printer/base.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IoMessage } from '../../toolkit/cli-io-host';
55
import { maxResourceTypeLength, stackEventHasErrorMessage } from '../../util';
66

77
export interface IActivityPrinter {
8-
notify<T>(msg: IoMessage<T>): void;
8+
notify(msg: IoMessage<unknown>): void;
99
}
1010

1111
export interface ActivityPrinterProps {
@@ -51,10 +51,10 @@ export abstract class ActivityPrinterBase implements IActivityPrinter {
5151
/**
5252
* Receive a stack activity message
5353
*/
54-
public notify(msg: IoMessage<any>): void {
54+
public notify(msg: IoMessage<unknown>): void {
5555
switch (msg.code) {
5656
case 'CDK_TOOLKIT_I5501':
57-
this.start(msg.data);
57+
this.start(msg.data as { stack: CloudFormationStackArtifact });
5858
break;
5959
case 'CDK_TOOLKIT_I5502':
6060
this.activity(msg.data as StackActivity);

packages/aws-cdk/lib/toolkit/cli-io-host.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class CliIoHost implements IIoHost {
127127

128128
// Corked Logging
129129
private corkedCounter = 0;
130-
private readonly corkedLoggingBuffer: IoMessage<any>[] = [];
130+
private readonly corkedLoggingBuffer: IoMessage<unknown>[] = [];
131131

132132
private constructor(props: CliIoHostProps = {}) {
133133
this.currentAction = props.currentAction ?? 'none';
@@ -220,7 +220,7 @@ export class CliIoHost implements IIoHost {
220220
* Notifies the host of a message.
221221
* The caller waits until the notification completes.
222222
*/
223-
public async notify<T>(msg: IoMessage<T>): Promise<void> {
223+
public async notify(msg: IoMessage<unknown>): Promise<void> {
224224
if (this._internalIoHost) {
225225
return this._internalIoHost.notify(msg);
226226
}
@@ -250,7 +250,7 @@ export class CliIoHost implements IIoHost {
250250
/**
251251
* Detect stack activity messages so they can be send to the printer.
252252
*/
253-
private isStackActivity(msg: IoMessage<any>) {
253+
private isStackActivity(msg: IoMessage<unknown>) {
254254
return [
255255
'CDK_TOOLKIT_I5501',
256256
'CDK_TOOLKIT_I5502',
@@ -376,7 +376,7 @@ export class CliIoHost implements IIoHost {
376376
/**
377377
* Formats a message for console output with optional color support
378378
*/
379-
private formatMessage(msg: IoMessage<any>): string {
379+
private formatMessage(msg: IoMessage<unknown>): string {
380380
// apply provided style or a default style if we're in TTY mode
381381
let message_text = this.isTTY
382382
? styleMap[msg.level](msg.message)

packages/aws-cdk/test/_helpers/test-io-host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class TestIoHost implements IIoHost {
1515
this.requestSpy = jest.fn();
1616
}
1717

18-
public async notify<T>(msg: IoMessage<T>): Promise<void> {
18+
public async notify(msg: IoMessage<unknown>): Promise<void> {
1919
if (isMessageRelevantForLevel(msg, this.level)) {
2020
this.notifySpy(msg);
2121
}

0 commit comments

Comments
 (0)