Skip to content

Commit 95bb5a5

Browse files
committed
chore: Fix provider-key access to avoid TS compile error (#2819)
1 parent 10b49c1 commit 95bb5a5

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

docs/dev-notes/2025-11-12/add_provider_key_for_contest_table_provider/plan.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,21 @@ private providers = new Map<string, ContestTableProviderBase>();
118118

119119
```typescript
120120
addProvider(provider: ContestTableProviderBase): this {
121-
const key = provider['getProviderKey']();
121+
const key = provider.getProviderKey();
122122
this.providers.set(key, provider);
123123

124124
return this;
125125
}
126126
```
127127

128-
**注**: `protected` メソッドへのアクセスのため、角括弧表記を使用
128+
**注**: `getProviderKey()` は public メソッドとして直接呼び出し可能
129129

130130
#### addProviders() メソッド修正
131131

132132
```typescript
133133
addProviders(...providers: ContestTableProviderBase[]): this {
134134
providers.forEach((provider) => {
135-
const key = provider['getProviderKey']();
135+
const key = provider.getProviderKey();
136136
this.providers.set(key, provider);
137137
});
138138
return this;
@@ -237,9 +237,10 @@ pnpm format src/lib/utils/contest_table_provider.ts
237237

238238
### 実装時の留意事項
239239

240-
1. **protected メソッドへのアクセス**
241-
- `getProviderKey()` は protected なため、Group 内では `provider['getProviderKey']()` で呼び出す
242-
- TypeScript の暗黙の型チェックを通すため、角括弧表記が必須
240+
1. **Public メソッドアクセス**
241+
- `getProviderKey()` は public メソッドとして実装
242+
- TypeScript strict mode で保護されており、アクセス制御が厳密に実施される
243+
- Protected メソッドへのアクセスは角括弧表記 `provider['method']()` でも strict mode では禁止
243244

244245
2. **後方互換性**
245246
- `getProvider(contestType)` で section 未指定時、複合キーなしで検索
@@ -302,10 +303,15 @@ pnpm format src/lib/utils/contest_table_provider.ts
302303
### 実装パターン
303304

304305
1. **ProviderKey 型の活用**: `type ProviderKey = \`${ContestType}\` | \`${ContestType}::${string}\`` でテンプレートリテラル型を定義し、メソッドの戻り値型として使用することで型安全性を向上
305-
2. **保護メソッドの外部アクセス**: Protected メソッドへの外部アクセスは角括弧表記 `provider['getProviderKey']()` で実装
306+
2. **Public メソッドアクセス**: Protected メソッドは TypeScript strict mode では直接アクセス不可。メソッドの責務と呼び出し元に応じて public/protected を適切に選択
306307
3. **複合キー設計**: 単純キー(ContestType)と複合キー(ContestType + section)の併存は後方互換性を損なわず拡張性を確保
307308
4. **静的ファクトリメソッド**: `createProviderKey()` を static メソッドで共通化することで、キー生成ロジックの一元管理を実現
308309

310+
### TypeScript Strict Mode
311+
312+
- **アクセス制御の厳密性**: Protected/private メンバーへのアクセスは角括弧表記でも strict mode では禁止
313+
- **推奨解決策**: public メソッドまたは公開 API として設計し、カプセル化を保ちながら必要な機能を公開
314+
309315
### テスト戦略
310316

311317
1. **複合キー検証**: 複数 section を持つ Provider の登録・取得を個別テストで検証

src/lib/utils/contest_table_provider.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ export abstract class ContestTableProviderBase implements ContestTableProvider {
5959
* Get this provider's key
6060
* Combines contestType and section to create a unique identifier
6161
*
62-
* @protected
6362
* @returns {ProviderKey} This provider's key
6463
*/
65-
protected getProviderKey(): ProviderKey {
64+
getProviderKey(): ProviderKey {
6665
return ContestTableProviderBase.createProviderKey(this.contestType, this.section);
6766
}
6867

@@ -546,8 +545,7 @@ export class ContestTableProviderGroup {
546545
* @returns Returns this for method chaining
547546
*/
548547
addProvider(provider: ContestTableProviderBase): this {
549-
// Access protected getProviderKey method using bracket notation
550-
const key = provider['getProviderKey']();
548+
const key = provider.getProviderKey();
551549
this.providers.set(key, provider);
552550
return this;
553551
}
@@ -561,8 +559,7 @@ export class ContestTableProviderGroup {
561559
*/
562560
addProviders(...providers: ContestTableProviderBase[]): this {
563561
providers.forEach((provider) => {
564-
// Access protected getProviderKey method using bracket notation
565-
const key = provider['getProviderKey']();
562+
const key = provider.getProviderKey();
566563
this.providers.set(key, provider);
567564
});
568565
return this;

0 commit comments

Comments
 (0)