-
Notifications
You must be signed in to change notification settings - Fork 12.8k
instanceof does not respect inherited Symbol.hasInstance
#56536
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
For the record, it actually works correctly with an inherited So the issue is specifically with intersections and generics. |
@fatcerberus well observed! Also broken with unions: const Top = {[Symbol.hasInstance]:(x:unknown)=>(true as const)}
const Bot = {[Symbol.hasInstance]:(x:unknown)=>(false as const)}
// broken
function myInstanceOf(x:unknown, c : typeof Top | typeof Bot)
{
return x instanceof c
} |
Abe27342
added a commit
to microsoft/FluidFramework
that referenced
this issue
Jul 16, 2024
## Description With the removal of concrete DDS classes, one pattern that's come up repeatedly is customer code which previously checked `instanceof MySharedObject` (usually when the code supported multiple shared object types for whatever reason), which no longer works. This change adds a drop-in replacement to the public API surface. ## Breaking Changes As `SharedObjectKind` was marked sealed, this is non-breaking. ## Alternatives Considered We could expose free functions in each package easily e.g. by using a helper like this: ```typescript export function createSharedObjectTypeguard<TSharedObject>( kind: ISharedObjectKind<TSharedObject>, ): (loadable: IFluidLoadable) => loadable is IFluidLoadable & TSharedObject { const factoryType = kind.getFactory().type; return (loadable: IFluidLoadable): loadable is IFluidLoadable & TSharedObject => { return isChannel(loadable) && loadable.attributes.type === factoryType; }; } ``` Ultimately this will be more code though and arguably less discoverable. We could also add back support for `instanceof` using `Symbol.hasInstance` (and the same implementation as `.is`), but due to microsoft/TypeScript#56536, this won't work for customers using TS below 5.5, so we'll need something else anyway at least for now. --------- Co-authored-by: Abram Sanderson <[email protected]> Co-authored-by: Craig Macomber (Microsoft) <[email protected]>
RishhiB
pushed a commit
to RishhiB/FluidFramework-1
that referenced
this issue
Jul 18, 2024
## Description With the removal of concrete DDS classes, one pattern that's come up repeatedly is customer code which previously checked `instanceof MySharedObject` (usually when the code supported multiple shared object types for whatever reason), which no longer works. This change adds a drop-in replacement to the public API surface. ## Breaking Changes As `SharedObjectKind` was marked sealed, this is non-breaking. ## Alternatives Considered We could expose free functions in each package easily e.g. by using a helper like this: ```typescript export function createSharedObjectTypeguard<TSharedObject>( kind: ISharedObjectKind<TSharedObject>, ): (loadable: IFluidLoadable) => loadable is IFluidLoadable & TSharedObject { const factoryType = kind.getFactory().type; return (loadable: IFluidLoadable): loadable is IFluidLoadable & TSharedObject => { return isChannel(loadable) && loadable.attributes.type === factoryType; }; } ``` Ultimately this will be more code though and arguably less discoverable. We could also add back support for `instanceof` using `Symbol.hasInstance` (and the same implementation as `.is`), but due to microsoft/TypeScript#56536, this won't work for customers using TS below 5.5, so we'll need something else anyway at least for now. --------- Co-authored-by: Abram Sanderson <[email protected]> Co-authored-by: Craig Macomber (Microsoft) <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π Search Terms
hasinstance, instanceof, 2359, TS2359
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?declaration=false&target=9&module=7&ts=5.4.0-dev.20231124#code/C4TwDgpgBAEghgZwJIDsHDigxhA8gMygF4oBvAKCigG0BlEAWwCMB7AGwDoALRVdTHAF0AFAA8AXAFcUAaxQsA7igCU4qK3YRM5AL7lyAegNQWM8vmlZgASxYooDEHwzY8+MVNnylAGihZxeGQ0FxwCZQoqACcIYEko+1EoaxCBCBZCLF19I3Uo0wgUc0sbOwcnVNcCVGAIKIQIK1sUD2k5RRQ-AKDnNIIoADITJgArRuAIyigYuISoJJT+Vwz-bMNjJnyZQuLsUvtHXqr8AHFCuussAB4AYSgIUVqUABMEWF5KsPwAPlavDq64huk2isXiiWSn3SmV0QA
π» Code
π Actual behavior
The second and third examples give error TS2359
In all cases, the right-hand side is an object type with a
Symbol.hasInstance
method.π Expected behavior
I expect all three examples to typecheck.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: