-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Narrowing this
using custom type predicate affected by TS 5.0
#53436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@Andarist can you check out this repo's |
If you experience the same problem with 4.9 then it's a different issue - since the issue that I reported herein a behavior change that was introduced 5.0 |
ok, i'll open a separate issue. It looks quite similar, i couldnβt be sure. |
Ran into a possibly related issue today with some relatively straightforward types: type AbstractableConstructor<instance = {}> = abstract new (
...args: never[]
) => instance
type InstanceOf<constructor extends AbstractableConstructor> = constructor extends AbstractableConstructor<infer instance> ? instance : never
export declare class ProtoNode<
proto extends AbstractableConstructor = AbstractableConstructor
> {
// Using the builtin InstanceType here unfortunately leads to any
// This PR would also be a huge help!
// https://github.com/microsoft/TypeScript/pull/55714
declare instance: InstanceOf<proto>
extendsOneOf<constructors extends readonly AbstractableConstructor[]>(
...constructors: constructors
): this is ProtoNode<constructors[number]>
}
const doSomething = (node: ProtoNode) => {
if (node.extendsOneOf(Array)) {
// Doesn't narrow node at all
return node
// ^? ProtoNode<AbstractableConstructor<{}>>
} else if (node.extendsOneOf(Date)) {
// Thinks this is impossible
return node
// ^? never
}
return false
} #55714 would be a big help as well, but even working around that it seems the type guard doesn't actually do any narrowing, causing TS to infer that it is always satisfied? Would be great to get a second look at this since the situations it leads to are very confusing to reason about. |
Bug Report
π Search Terms
narrowing this predicate
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
state
gets narrowed down tonever
in the else branchπ Expected behavior
state
to only get narrowed down to the appropriate value within theif
branchcc @ahejlsberg
The text was updated successfully, but these errors were encountered: