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
Repository metrics
- Stars
- (48,455 stars)
- PR merge metrics
- (Avg merge 6d 17h) (9 merged PRs in 30d)
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