Skip to content
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

Re-exporting the Record module creates a runtime exception #1249

Closed
Magellol opened this issue Jun 23, 2020 · 8 comments
Closed

Re-exporting the Record module creates a runtime exception #1249

Magellol opened this issue Jun 23, 2020 · 8 comments
Assignees
Labels
Milestone

Comments

@Magellol
Copy link

🐛 Bug report

Current Behavior

We usually "extend" some modules with our own operators and we do this by re-exporting the module from another file such as:

// modules/Record/index.ts
export * from 'fp-ts/lib/Record';
export * from './operators';

// modules/Record/operators.ts
import * as R from 'fp-ts/lib/Record';

export const fn = () => {}

So in the codebase, we can do:

import * as R from 'modules/Record';

R.fn();
R.keys();
...

We've been doing that for different modules such as Option, Array, Either etc and all is good but for some reason re-exporting Record creates the following runtime exception

image

Expected behavior

No runtime exception. Allowing us to re-export the module to extend it.

Your environment

Software Version(s)
fp-ts 2.6.5
TypeScript 3.9.2
@Magellol
Copy link
Author

Magellol commented Jun 23, 2020

@OliverJAsh ran some tests and it may potentially be because of the following code generated by TS

var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
}

https://www.typescriptlang.org/play/index.html?module=1&ssl=1&ssc=1&pln=1&pc=20#code/KYDwDg9gTgLgBAKjgMyhAtnA5MiEtA

In this case, it feels like the export.hasOwnProperty used by the snippet is pointing at the hasOwnProperty from ReadOnlyRecord

fp-ts/src/ReadonlyRecord.ts

Lines 162 to 164 in 1b0ee81

export function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K {
return _hasOwnProperty.call(r, k)
}

Looks like renaming it should fix the issue

Ref on the TS repo: microsoft/TypeScript#3197

@OliverJAsh OliverJAsh added the bug label Jun 30, 2020
@gcanti
Copy link
Owner

gcanti commented Jul 1, 2020

@OliverJAsh RE: the "bug" label. It doesn't look fp-ts's fault

@OliverJAsh
Copy link
Collaborator

Yeah you're right. Although the TS team have already said they won't fix this, so I guess it's on us to workaround it instead? microsoft/TypeScript#3197 (comment)

@gcanti
Copy link
Owner

gcanti commented Jul 1, 2020

the TS team have already said they won't fix this

I see. I can't rename hasOwnProperty because would be a breaking change.

However, as a fix, I can try to handle this

const _hasOwnProperty = Object.prototype.hasOwnProperty

export function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K
export function hasOwnProperty<K extends string>(this: any, k: string, r?: ReadonlyRecord<K, unknown>): k is K {
  return _hasOwnProperty.call(r === undefined ? this : r, k)
}

what do you think?

@OliverJAsh
Copy link
Collaborator

That should work!

@gcanti gcanti self-assigned this Jul 1, 2020
@gcanti gcanti added this to the 2.7 milestone Jul 1, 2020
@gcanti gcanti closed this as completed in b8a11fa Jul 1, 2020
@OliverJAsh
Copy link
Collaborator

This bug is happening again in 2.10.0 when we try to call Record.filterMap. It looks like it's because of this change: 02e0d21#diff-2a28f9bcbc06ac11c7768967193244d8f9ca05a4e96225ca8449930aa26870b0R663.

We need to apply the same fix to hasOwnProperty here:

export const hasOwnProperty = Object.prototype.hasOwnProperty

I'll raise a PR.

@gcanti
Copy link
Owner

gcanti commented Apr 15, 2021

@OliverJAsh damn, I forgot again this issue, I'm sorry :(

This is an internal function so we can rename it (to has for example) without any worries

@OliverJAsh
Copy link
Collaborator

Unfortunately the issue persists in 2.10.1. I think this change is also causing an issue: 281dc29#diff-dc4b4fb0cd6e06c8bf587eb2f8817ffa1ed096efe2c6c5175d3604fb57d3a96cR796

Record.hasOwnProperty no longer handles this because it's now just an alias for RR.has, and RR.has doesn't handle this anymore: 281dc29#diff-2a28f9bcbc06ac11c7768967193244d8f9ca05a4e96225ca8449930aa26870b0L168

I'm not sure how we should fix this. WDYT @gcanti?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants