You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 .
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:
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().
⚙ Compilation target
ESNext (ES2024?)
⚙ Library
typescript 5.7.3
Missing / Incorrect Definition
Sample Code
Documentation Link
https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype
The text was updated successfully, but these errors were encountered: