错误 TS2532:在检查是否已定义之后,对象可能是’undefined’

huangapple go评论65阅读模式
英文:

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(&quot;sha1&quot;).update(json).digest(&quot;hex&quot;);
    }
}

type PageUtilStep = &quot;backward&quot; | &quot;forward&quot;;

Here is the code where a get the typescript error on the return line:

export default class TattooLoader {
    private artistCache: Record&lt;string, DataLoader&lt;number, TattooEntity[]&gt;&gt;;

    public constructor() {
        this.artistCache = {};
    }

    public async fillArtist(artist: number, pageUtil: PageUtil): Promise&lt;TattooEntity[]&gt; {
        const hash = pageUtil.createHash();

        if (!this.artistCache[hash]) {
            this.artistCache[hash] = new DataLoader(async (artists) =&gt; this.batchArtists(artists, pageUtil));
        }

        return this.artistCache[hash].load(artist);
    }

    private async batchArtists(artists: readonly number[], pageUtil: PageUtil): Promise&lt;TattooEntity[][]&gt; {
        ...
    }
}

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&lt;TattooEntity[]&gt; {
        const hash = pageUtil.createHash();

        if (this.artistCache[hash]) {
            return this.artistCache[hash].load(artist);
        }

        this.artistCache[hash] = new DataLoader(async (artists) =&gt; 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) {
  ...
}

huangapple
  • 本文由 发表于 2023年5月13日 20:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定