Hi @Newbie012
It would be great to be able to use intersection types with existing object type aliases to be able to transform existing types instead of copying them throughout the codebase:
type User = {
id: number;
username: string;
};
type UserWithPasswordHash = User & {
passwordHash: string;
};
export async function getUserWithPasswordHashById(id: number) {
const [user] = await sql<UserWithPasswordHash[]>`
SELECT
id,
username,
password_hash
FROM
users
WHERE
id = ${id}
`;
return user;
}
Right now, this leads to a crash of ESLint with a confusing error message (as seen here: https://github.com/upleveled/security-vulnerability-examples-next-js-postgres/actions/runs/3330517524/jobs/5509108053):
TypeError: Cannot read properties of undefined (reading 'members')
Occurred while linting /home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/database/users.ts:24
Rule: "@ts-safeql/check-sql"
at tsTypeToText (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/utils/ts.utils.js:18:34)
at tsTypeToText (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/utils/ts.utils.js:22:16)
at getTypeAnnotationState (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/rules/check-sql.rule.js:271:49)
at /home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/rules/check-sql.rule.js:206:37
at /home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/fp-ts/lib/Either.js:863:60
at pipe (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/fp-ts/lib/function.js:300:20)
at reportCheck (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/rules/check-sql.rule.js:173:29)
at checkConnectionByTagExpression (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/rules/check-sql.rule.js:236:16)
at /home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/@ts-safeql/eslint-plugin/lib/rules/check-sql.rule.js:160:16
at e.run (/home/runner/work/security-vulnerability-examples-next-js-postgres/security-vulnerability-examples-next-js-postgres/node_modules/ts-pattern/dist/index.cjs:1:4927)
error Command failed with exit code 2.
It would be great if the intersection types worked
Workaround:
Copy the properties from the other type to the new type (decreasing maintainability):
type User = {
id: number;
username: string;
};
type UserWithPasswordHash = {
id: number;
username: string;
passwordHash: string;
};
export async function getUserWithPasswordHashById(id: number) {
const [user] = await sql<UserWithPasswordHash[]>`
SELECT
id,
username,
password_hash
FROM
users
WHERE
id = ${id}
`;
return user;
}
--
Thanks for the new changes in the release @ts-safeql/[email protected]
, I'll try them out!
@ts-safeql/[email protected]
works great, thanks!
Owner Name | ts-safeql |
Repo Name | safeql |
Full Name | ts-safeql/safeql |
Language | TypeScript |
Created Date | 2022-09-08 |
Updated Date | 2023-03-16 |
Star Count | 795 |
Watcher Count | 5 |
Fork Count | 14 |
Issue Count | 7 |
Issue Title | Created Date | Updated Date |
---|