I am a fan of script languages and was using javascript for long time. But for a new project I decided to see how ts may help my development or maintaining part of the project. My biggest problem atm is finding types. Let me give an example. I am using drizzle for orm. For some reason I need to store the findMany query config. To be more clear I am pasting the whole type of the findMany function and class here.
export declare class RelationalQueryBuilder<TSchema extends TablesRelationalConfig, TFields extends TableRelationalConfig> {
private fullSchema;
private schema;
private tableNamesMap;
private table;
private tableConfig;
private dialect;
private session;
static readonly [entityKind]: string;
constructor(fullSchema: Record<string, unknown>, schema: TSchema, tableNamesMap: Record<string, string>, table: PgTable, tableConfig: TableRelationalConfig, dialect: PgDialect, session: PgSession);
findMany<TConfig extends DBQueryConfig<'many', true, TSchema, TFields>>(config?: KnownKeysOnly<TConfig, DBQueryConfig<'many', true, TSchema, TFields>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig>[]>;
findFirst<TSelection extends Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>(config?: KnownKeysOnly<TSelection, Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TSelection> | undefined>;
}
So let say I have my own class and I want a function that accepts exact same config type. How can I do that ?
Another question is while using findMany function I am not passing any type. For example :
await db.query.example.findMany({
where: (posts, { eq }) => (eq(posts.id, 1)),
});
What I expect here is something like this :
await db.query.example.findMany<SomeTypeHere>({
where: (posts, { eq }) => (eq(posts.id, 1)),
});
Since I am not passing any type what exactly going on here ? How it identifies the generic type ?
My last question is that let say I have a class and a method that accepting the config object. And inside the class I want to store the config value that passed to the function. What is the type of the variable ?
So anyone can provide a basic class that has a single config attribute and a function that accepts config parameter and sets it to class variable (Ofc with proper types)?
I think it should not be that hard. What I am missing here ?