Skip to content

Commit a4a9f97

Browse files
committed
fix: add type narrowing for tipsStorage index access (fix TS7053)
1 parent fbb1c08 commit a4a9f97

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

src/extension/services/tipsNotificationsService/tipsNotificationService.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { OutputChannelLogger } from "../../log/OutputChannelLogger";
1313
import { areSameDates, getRandomIntInclusive } from "../../../common/utils";
1414
import tipsStorage from "./tipsStorage";
1515

16+
type GeneralTipKey = keyof typeof tipsStorage.generalTips;
17+
type SpecificTipKey = keyof typeof tipsStorage.specificTips;
18+
1619
enum TipNotificationAction {
1720
GET_MORE_INFO = "tipsMoreInfo",
1821
DO_NOT_SHOW_AGAIN = "tipsDoNotShow",
@@ -143,15 +146,15 @@ export class TipNotificationService implements vscode.Disposable {
143146
}
144147

145148
public async setKnownDateForFeatureById(
146-
key: string,
149+
key: GeneralTipKey | SpecificTipKey,
147150
isGeneralTip: boolean = true,
148151
): Promise<void> {
149152
await this.initializeTipsConfig();
150153

151154
if (isGeneralTip) {
152-
this.tipsConfig.tips.generalTips[key].knownDate = new Date();
155+
this.tipsConfig.tips.generalTips[key as GeneralTipKey].knownDate = new Date();
153156
} else {
154-
this.tipsConfig.tips.specificTips[key].knownDate = new Date();
157+
this.tipsConfig.tips.specificTips[key as SpecificTipKey].knownDate = new Date();
155158
}
156159

157160
ExtensionConfigManager.config.set(this.TIPS_CONFIG_NAME, this.tipsConfig);
@@ -329,7 +332,9 @@ export class TipNotificationService implements vscode.Disposable {
329332
}
330333

331334
const randIndex: number = getRandomIntInclusive(leftIndex, generalTipsForRandom.length - 1);
332-
const selectedGeneralTipKey: string = generalTipsForRandom[randIndex];
335+
const selectedGeneralTipKey: GeneralTipKey = generalTipsForRandom[
336+
randIndex
337+
] as GeneralTipKey;
333338
const tipNotificationText = this.getGeneralTipNotificationTextByKey(selectedGeneralTipKey);
334339

335340
this.tipsConfig.tips.generalTips[selectedGeneralTipKey].shownDate = new Date();
@@ -361,7 +366,7 @@ export class TipNotificationService implements vscode.Disposable {
361366
}
362367

363368
private async showSpecificTipNotification(
364-
tipKey: string,
369+
tipKey: SpecificTipKey,
365370
): Promise<GeneratedTipResponse | undefined> {
366371
if (this.tipsConfig.tips.specificTips[tipKey].shownDate) {
367372
return;
@@ -393,19 +398,19 @@ export class TipNotificationService implements vscode.Disposable {
393398
return tipsConfig;
394399
}
395400

396-
private getGeneralTipNotificationTextByKey(key: string): string {
401+
private getGeneralTipNotificationTextByKey(key: GeneralTipKey): string {
397402
return tipsStorage.generalTips[key].text;
398403
}
399404

400-
private getSpecificTipNotificationTextByKey(key: string): string {
405+
private getSpecificTipNotificationTextByKey(key: SpecificTipKey): string {
401406
return tipsStorage.specificTips[key].text;
402407
}
403408

404-
private getGeneralTipNotificationAnchorLinkByKey(key: string): string {
409+
private getGeneralTipNotificationAnchorLinkByKey(key: GeneralTipKey): string {
405410
return tipsStorage.generalTips[key].anchorLink;
406411
}
407412

408-
private getSpecificTipNotificationAnchorLinkByKey(key: string): string {
413+
private getSpecificTipNotificationAnchorLinkByKey(key: SpecificTipKey): string {
409414
return tipsStorage.specificTips[key].anchorLink;
410415
}
411416

test/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export async function run(): Promise<void> {
7373
});
7474
})
7575
.finally(() => {
76-
if (nyc) {
77-
nyc.writeCoverageFile();
78-
return nyc.report();
79-
}
80-
return void 0;
76+
if (nyc) {
77+
nyc.writeCoverageFile();
78+
return nyc.report();
79+
}
80+
return void 0;
8181
});
8282
}

0 commit comments

Comments
 (0)