Microsoft/TypeScript
GitHub ã§èŠãAssertion type function not working as expected with everything but ES3 since >=4.9.4
Open
#52,430 opened on 2023幎1æ26æ¥
BugDomain: check: Control FlowHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
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