FormidableLabs / react-fast-compare

fastest deep equal comparison for React
MIT License
1.59k stars 54 forks source link

Fix incorrect Typescript types #62

Closed henrinormak closed 4 years ago

henrinormak commented 4 years ago

While trying to use this from TS, I noticed that I got the following error: a.default is not a function, which hinted at some import problem with this package.

After looking at the source code (specifically this line), it is evident that this package is what TS calls a "module library" - http://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#identifying-a-module-library-from-code). Looking at other similar packages (namely request), you can see that their type definitions are not using export default.

As such, it seems that the types in this package should be changed:

declare function isEqual(a: any, b: any): boolean;
declare namespace isEqual {}
export = isEqual;
chrisbolin commented 4 years ago

just as an example, here's express' type declarations in DefinitelyTyped

chrisbolin commented 4 years ago

I think the original premise of this issue might be a little off.

a.default is not a function

That is correct: https://github.com/FormidableLabs/react-fast-compare/blob/master/index.js#L118 We don't do a default export. We instead assign module.exports to the function.

chrisbolin commented 4 years ago

after looking at this, the key flaw that this PR fixes is

export default isEqual

There is no default export in the module, so the typing was off. Hence the export=isEqual

chrisbolin commented 4 years ago

@henrinormak can you explain the purpose of

declare namespace isEqual {}

I'm pretty sure it's just dead code, especially after talking with @kitten.