英文:
Error TS2532: Object is possibly 'undefined' after checking if it is defined
问题
以下是帮助类创建哈希的代码部分的翻译:
export default class PageUtil {
private size: number;
private step: PageUtilStep;
private cursor: unknown[] | undefined;
public constructor(size: number, step: PageUtilStep, cursor?: unknown[]) {
this.size = size;
this.step = step;
this.cursor = cursor;
}
public createHash(): string {
const json = JSON.stringify([this.size, this.step, this.cursor]);
return createHash("sha1").update(json).digest("hex");
}
}
type PageUtilStep = "backward" | "forward";
以下是在返回行上出现 TypeScript 错误的代码部分的翻译:
export default class TattooLoader {
private artistCache: Record<string, DataLoader<number, TattooEntity[]>>;
public constructor() {
this.artistCache = {};
}
public async fillArtist(artist: number, pageUtil: PageUtil): Promise<TattooEntity[]> {
const hash = pageUtil.createHash();
if (!this.artistCache[hash]) {
this.artistCache[hash] = new DataLoader(async (artists) => this.batchArtists(artists, pageUtil));
}
return this.artistCache[hash].load(artist);
}
private async batchArtists(artists: readonly number[], pageUtil: PageUtil): Promise<TattooEntity[][]> {
// 其他代码部分...
}
}
如果要更具体地测试是否为 undefined 而更改代码,以下是翻译后的版本:
public async fillArtist(artist: number, pageUtil: PageUtil): Promise<TattooEntity[]> {
const hash = pageUtil.createHash();
if (this.artistCache[hash]) {
return this.artistCache[hash].load(artist);
}
this.artistCache[hash] = new DataLoader(async (artists) => this.batchArtists(artists, pageUtil));
return this.artistCache[hash].load(artist);
}
英文:
Here is the code for a helper class that creates a hash:
export default class PageUtil {
private size: number;
private step: PageUtilStep;
private cursor: unknown[] | undefined;
public constructor(size: number, step: PageUtilStep, cursor?: unknown[]) {
this.size = size;
this.step = step;
this.cursor = cursor;
}
public createHash(): string {
const json = JSON.stringify([this.size, this.step, this.cursor]);
return createHash("sha1").update(json).digest("hex");
}
}
type PageUtilStep = "backward" | "forward";
Here is the code where a get the typescript error on the return line:
export default class TattooLoader {
private artistCache: Record<string, DataLoader<number, TattooEntity[]>>;
public constructor() {
this.artistCache = {};
}
public async fillArtist(artist: number, pageUtil: PageUtil): Promise<TattooEntity[]> {
const hash = pageUtil.createHash();
if (!this.artistCache[hash]) {
this.artistCache[hash] = new DataLoader(async (artists) => this.batchArtists(artists, pageUtil));
}
return this.artistCache[hash].load(artist);
}
private async batchArtists(artists: readonly number[], pageUtil: PageUtil): Promise<TattooEntity[][]> {
...
}
}
I don't understand because a get this error after to test if this.artistCache[hash]
is undefined
and create it if so.
If I change the code to be more specific to test if it's undefined I get the same error:
public async fillArtist(artist: number, pageUtil: PageUtil): Promise<TattooEntity[]> {
const hash = pageUtil.createHash();
if (this.artistCache[hash]) {
return this.artistCache[hash].load(artist);
}
this.artistCache[hash] = new DataLoader(async (artists) => this.batchArtists(artists, pageUtil));
return this.artistCache[hash].load(artist);
}
答案1
得分: 2
Indexed access doesn't narrow the undefined
.
You'll have to create an intermediate variable:
const loader = this.artistCache[hash]
if (loader) {
...
}
英文:
Indexed access doesn't narrow the undefined
.
You'll have to create an intermediate variable :
const loader = this.artistCache[hash]
if(loader) {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论