sqlc-dev / sqlc-gen-typescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Row is possibly undefined

woowenjun99 opened this issue · comments

This is not a bug but more of an annoying issue. Upon generating the TypeScript file, there is an issue where row might be undefined during const row = result.rows[0];. One simple solution would be to typecast by editing the generated file, but it is not really ideal. Would there be a better solution to this?

Screenshot 2024-01-03 at 12 39 57 AM Screenshot 2024-01-03 at 12 39 52 AM

We can update the generated code with an additional if statement. Can you edit the code to add the following if statement and see if things are fixed?

    if (result.rows.length !== 1) {
        return null;
    }
    const row = result.rows[0];
    if (!row) {
        return null;
    }
    return {
        id: row[0],
        name: row[1],
        bio: row[2]
    };

Yeap adding in the additional if statement does remove the error. However, it leads to another linting issue of unsafe assignment. I can disable the lint for this.

Screenshot 2024-01-03 at 8 15 21 AM