krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
18.1k stars 766 forks source link

ERROR in node_modules/fuse.js/index.d.ts(12,49): error TS1144: '{' or ';' expected. #285

Closed migvill closed 4 years ago

migvill commented 5 years ago

I get a webpack compile error when I import Fuse on an Angular project

ERROR in node_modules/fuse.js/index.d.ts(12,49): error TS1144: '{' or ';' expected.
node_modules/fuse.js/index.d.ts(12,57): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
node_modules/fuse.js/index.d.ts(12,69): error TS1005: ';' expected.
node_modules/fuse.js/index.d.ts(12,73): error TS1128: Declaration or statement expected.
node_modules/fuse.js/index.d.ts(13,7): error TS1005: ';' expected.
node_modules/fuse.js/index.d.ts(13,38): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(13,64): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(13,108): error TS1005: ';' expected.
node_modules/fuse.js/index.d.ts(14,7): error TS1005: ';' expected.
node_modules/fuse.js/index.d.ts(14,38): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(14,64): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(16,21): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(16,39): error TS1005: '(' expected.
node_modules/fuse.js/index.d.ts(16,40): error TS1005: ',' expected.
node_modules/fuse.js/index.d.ts(16,58): error TS1005: '(' expected.
node_modules/fuse.js/index.d.ts(17,1): error TS1128: Declaration or statement expected.

It seems the Typescript is invalid. Does anyone know how to fix this? Or workaround this issue?

chrisbendel commented 5 years ago

This looks like it was related to the latest release (https://github.com/krisk/Fuse/pull/281/files)

Our team just ran into the same issue. Hard locking our fuse.js version in package.json fixed the issue for us. We're using typescript version 2.7.2 by the way

Previous

"fuse.js": "^3.2.1"

Fixed

"fuse.js": "3.2.1"
krisk commented 5 years ago

Thanks for flagging.

@gatimus wanna take a look?

mina-skunk commented 5 years ago

@migvill what is the version of typescript?

mina-skunk commented 5 years ago

Looks like the solution I came up with for #265 uses Conditional Types which were introduced in TypeScript 2.8. So Fuse will have to require TypeScript 2.8 or (to support 2.7) we can change the definition to:

search(pattern: string, opts?: SearchOpts): (string | T | Fuse.FuseResult<string> | Fuse.FuseResult<T>)[];

which would require type checks after getting to result. Something like:

function isFuseResult<T>(result: any): result is Fuse.FuseResult<T> {
  return typeof result === 'object' && result['item'];
}

const results = fuse.search('/* search term */');
for (let i = 0; i < results.length; i++) {
  const result = results[i];
  if (isFuseResult(result)) {
    if (typeof result.item === 'string') {
      result.item; // > string (id)
    } else {
      result.item; // > T
    }
  } else {
    if (typeof result === 'string') {
      result; // > string (id)
    } else {
      result; // > T
    }
  }
}

Which could get messy fast. Or could return any:

search(pattern: string, opts?: SearchOpts): any[];

and let the developer assert / cast the type. All 3 options are opinionated.

migvill commented 5 years ago

@migvill what is the version of typescript?

@gatimus Sorry for late reply, typescript 2.6.2

This looks like it was related to the latest release (https://github.com/krisk/Fuse/pull/281/files)

Our team just ran into the same issue. Hard locking our fuse.js version in package.json fixed the issue for us. We're using typescript version 2.7.2 by the way

Previous

"fuse.js": "^3.2.1"

Fixed

"fuse.js": "3.2.1"

@chrisbendel Thanks for the tip, I was able to get my build working by using "fuse.js": "3.3.1"

mina-skunk commented 5 years ago

I know Current version requires TypeScript 2.8, this is because Conditional Types is required to accurately represent fues.js behavior in TypeScript. Need maintainer input on if this desirable or if alternative(s) is acceptable.

favna commented 5 years ago

I for one see no reason whatsoever not to use TypeScript v3.3 at this point considering by ways of optional compiler flags there isnothing breaking between v2 and v3.

Furthermore any should be avoided in TypeScript wherever possible. If you're going to use any you might as well roll back to regular JavaScript since you're losing the very thing TypeScript meant to primarily fix and thus you're going against the core TypeScript mentality.

I'd say this issue can be closed and people who run into this issue should update their TypeScript.


As an aside the given code example a few comments earlier is also ES5 - now this is an opinion but considering you can just set your target to ES5 and your lib in, for example, esnext there is no reason not to use powerful ES6 features like Array.forEach() and expressed arrow functions (const someFunc = () => someReturnValue)

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

mina-skunk commented 4 years ago

@krisk this could be closed if you are ok with saying Fuse requires TypeScript 2.8 or newer (current is 3.8).