Hi @Newbie012 , hope you are well!
It would be great to be able to use generic utility types such as Pick
and Omit
with existing types to be able to transform existing types instead of copying them throughout the codebase:
type User = {
id: number;
username: string;
}
export async function getUserByUsername(username: string) {
const [user] = await sql<Pick<User, 'id'>[]>`
SELECT
id
FROM
users
WHERE
username = ${username}
`;
return user;
}
Right now, this leads to a confusing error message:
Query has incorrect type annotation.
Expected: null[]`
Actual: { id: number; }[]
eslint @ts-safeql/check-sql
It would be great if the utility types worked
Workaround:
Use the literal object type instead of the utility type (decreasing maintainability):
export async function getUserByUsername(username: string) {
const [user] = await sql<{ id: number }[]>`
SELECT
id
FROM
users
WHERE
username = ${username}
`;
return user;
}
--
It seems like TypeScript doesn't expose its API if the type is identical to another one, so I created a question in Stack Overflow
Until then, I'll write my own isIdentical
function, it should initially support:
Once it's complete, I'll be able to close #93 as well.
@orta mentioned that there are some prior art for testing types here: effectivetypescript.com/2022/05/28/eslint-plugin-expect-type
I did a little digging at the source code of eslint-plugin-expect-type
(the one that is in the article), and the "magic-souce" is the following lines:
cosnt qi = languageService.getQuickInfoAtPosition(sourceFile.fileName, tad.getStart());
const actual = qi.displayParts.map((dp) => dp.text).join("");
The thing is, It's very accurate when you need to get the type definition of a reference.
It's so accurate that when you want to know what fn<Pick<Person, "name">>()
is, it returns type Pick<T, K extends keyof T> = { [P in K]: T[P]; }
.
But, if I assign that type to a new type (type PickPersonName = Pick<Person, "name">
) it will work.
Another problematic approach with getQuickInfoAtPosition
:
type Starship = { id: number; name: string; captain_id: number | null; };
type Type = Pick<Starship, "captain_id" | "id"> & { x: number; }
function fn<T>() {};
fn<Type>();
// ?^ type Type = Pick<Starship, "captain_id" | "id"> & { x: number; }
?
comes before ^
in this comment? In the other example, the ^
was before the ?
_
"type resolver" type trick used in Yup (or anything similar) help here?Great! I'll try out the new changes in the release @ts-safeql/[email protected]
!
What's the quick summary? (just for anyone else who is trying to do something similar)
I imagine it has to do with these getTypeProperties
and toInlineLiteralTypeString
functions exported in packages/eslint-plugin/src/utils/get-type-properties.ts
?
@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 |
---|