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

Add typescript definition #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare namespace fuzzysearch {

}

/**
* The method will return true only if each character in the needle can be found in the haystack and occurs after the preceding matches.
* @param needle {string} the search value you're looking for
* @param haystack {string} find one or more occurrences of the "needle" within the "haystack"
* @example fuzzysearch('twl', 'cartwheel') // <- true
* @returns {boolean} `true` if needle matches haystack using a fuzzy-searching algorithm
*/
declare function fuzzysearch(needle: string, haystack: string): boolean;

export = fuzzysearch;
Copy link

@jskrnbindra jskrnbindra Apr 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think

export default fuzzysearch;

would be better here.
@dderevjanik

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it, but now I'm getting this error

[ts] Cannot invoke an expression whose type lacks a call signature. Type 'typeof 'fuzzysearch'' has no compatible call signatures.

export = fuzzysearch works as expected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jskrnbindra @dderevjanik Without the empty namespace but with the default export it should work.
The default export would match the libraries real export, I think.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As weird as it looks, these typings are correct as-is. (It matches the underlying implementation of module.exports = fuzzysearch;)

Writing the types as a default export would work in some cases but not others, depending on which module loader a given project is using. Would love to see this get merged! 🙏

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"email": "ng@bevacqua.io",
"url": "http://bevacqua.io"
},
"typings": "./index.d.ts",
"license": "MIT",
"repository": {
"type": "git",
Expand Down