Microsoft/TypeScript
Vedi su GitHubAssertion type function not working as expected with everything but ES3 since >=4.9.4
Open
#52.430 aperta il 26 gen 2023
BugDomain: check: Control FlowHelp Wanted
Metriche repository
- Star
- (48.455 star)
- Metriche merge PR
- (Merge medio 6g 17h) (9 PR mergiate in 30 g)
Descrizione
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