Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InstanceType<BigIntConstructor> fails with a compilation error and doesn't result in a bigint, as it's supposed to #61272

Closed
WillAvudim opened this issue Feb 26, 2025 · 3 comments

Comments

@WillAvudim
Copy link

WillAvudim commented Feb 26, 2025

⚙ Compilation target

ESNext (ES2024?)

⚙ Library

typescript 5.7.3

Missing / Incorrect Definition

InstanceType is a built-in utility type that resolves a constructor into the type being constructed.

E.g. InstanceType<StringConstructor> = string, InstanceType<BooleanConstructor> = boolean, InstanceType<DateConstructor> = Date, etc

However, it errors out for InstanceType<BigIntConstructor> with the error:
Type does not satisfy the constraint abstract new (...args: any) => any .

Sample Code

InstanceType<BigIntConstructor>

Documentation Link

https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype

@IllusionMH
Copy link
Contributor

Because it's not "newable" - throws in runtime if called with new https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#constructor

@WillAvudim
Copy link
Author

WillAvudim commented Feb 26, 2025

It doesn't have to be. <...>Constructor is normally used as an indication of the underlying type, and not the means to construct an object. We are discussing Typescript, after all.
E.g. if you are designing an API for prepared statements in SQL, you could use string literals:

PrepareSqliteStatement`SELECT column_1 FROM table_1 WHERE id = ${BigInt} AND id2 = ${String} `

And that translates into a function that accepts Fn(arg1: bigint, arg2: string), which invokes the prepared SQL statement:

type ConstructorArrayToTypeArray<T extends any[]> = {
  [K in keyof T]: InstanceType<T[K]>
}

export function PrepareSqliteStatement<Args extends any[]>(fragments: TemplateStringsArray, ...args: Args): (Fn: (Exec: (...args: ConstructorArrayToTypeArray<Args>) => any) => void) => void {

  return (Fn: (Exec: (...args: ConstructorArrayToTypeArray<Args>) => any) => void) => {
    let statement: PreparedStatement | undefined = sqlite.prepare(fragments.join("?"))

    try {

      Fn((...args: ConstructorArrayToTypeArray<Args>) => {
        statement!.bind(args as any)
        statement!.step()
        statement!.reset()
      })

    } finally {
      if (statement) {
        statement.finalize()
        statement = undefined
      }
    }
  }
}

@snarbles2
Copy link

In typescript, InstanceType<T> doesn't refer to some vague abstract notion of construction. It refers specifically to what you get when you call new T().

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

No branches or pull requests

3 participants