Microsoft/TypeScript
View on GitHubAssertion type function not working as expected with everything but ES3 since >=4.9.4
Open
#52,430 opened on Jan 26, 2023
BugDomain: check: Control FlowHelp Wanted
Description
Bug Report
๐ Search Terms
Assert, Assert Function, Assert Class Constructor
๐ Version & Regression Information
>=4.9.4
- This is a crash
- This changed between versions 4.8.4 and >=4.9.4
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because it works as expected
โฏ Playground Link
๐ป Code
export type AbstractType<T = any> = (abstract new (...args: any[]) => T) & Function;
export type ConcreteType<T = any> = (new (...args: any[]) => T) & Function;
export type Type<T = any> = AbstractType<T> | ConcreteType<T>;
export interface AbstractAssertions {
instance<Expects extends Type<unknown>>(
actual: unknown,
expects: Expects,
): asserts actual is InstanceType<Expects>;
}
export const assert: AbstractAssertions = {
instance(actual, expects) {
return actual instanceof expects;
},
};
let x = 1 as any
assert.instance(x, Error) // Error here
assert.instance(x, Date) // Error here
๐ Actual behavior
Show Type Error when not supposed to
๐ Expected behavior
Assert type as expected