Skip to content

Conversation

@chaucerbao
Copy link
Contributor

The definition of the type-generic used on the sql() function is:

sql<T extends Record<string, unknown> = Record<string, any>>

The Record<string, unknown> is an alias for the { [key: string]: unknown } type signature. The problem is that interface definitions don't include this type signature, so they cannot be passed into this type-generic.

Here's a standalone example:

interface MyInterface {
  name: string
}

const myFunc = <T extends Record<string, unknown> = Record<string, any>>(
  param: T,
) => undefined

const myInterface: MyInterface = {
  name: 'Name',
}

myFunc<MyInterface>(myInterface)

This will produce a type-check error:

Type 'MyInterface' does not satisfy the constraint 'Record<string, unknown>'.
Index signature for type 'string' is missing in type 'MyInterface'. [2344]

Copy link
Member

@DjDeveloperr DjDeveloperr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

@DjDeveloperr DjDeveloperr merged commit c078d61 into denodrivers:main Dec 18, 2024
4 of 5 checks passed
@varanauskas
Copy link
Contributor

@chaucerbao any reason T extends Record<string, any> was chosen instead of T extends Record<keyof T, unknown>

I try to almost avoid anys in my TypeScript, and in my limited testing Record<keyof T, unknown> is sufficient to prevent type errors with interfaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants