Microsoft/TypeScript
在 GitHub 查看Assertion type function not working as expected with everything but ES3 since >=4.9.4
Open
#52,430 创建于 2023年1月26日
BugDomain: check: Control FlowHelp Wanted
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
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