farzher / fuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

Home Page:https://rawgit.com/farzher/fuzzysort/master/test/test.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript error when I use keys

MartialSeron opened this issue · comments

Hi,

Thank you for your work. You did a great job!

I just want to known how to use keys with typescript because I got an error.

 [{
	"resource": "/workspace/src/script.ts",
	"owner": "typescript",
	"code": "2345",
	"severity": 8,
	"message": "Argument of type 'KeysResult<{ registrationNumber: string | null; realname: string | null; firstname: string | null; }>' is not assignable to parameter of type 'Result'.\n  Property 'target' is missing in type 'KeysResult<{ registrationNumber: string | null; realname: string | null; firstname: string | null; }>' but required in type 'Result'.",
	"source": "ts",
	"startLineNumber": 108,
	"startColumn": 38,
	"endLineNumber": 108,
	"endColumn": 44,
	"relatedInformation": [
		{
			"startLineNumber": 11,
			"startColumn": 14,
			"endLineNumber": 11,
			"endColumn": 20,
			"message": "'target' is declared here.",
			"resource": "/workspace/node_modules/fuzzysort/index.d.ts"
		}
	]
}]

here's my code

server.get('/fuzzysort', async (req, res) => {
  const { q } = req.query as { q: string };

  const users = await prisma.users.findMany({
    take: 5,
    where: {
      is_active: true,
      is_deleted: false
    },
    select: {
      registrationNumber: true,
      realname: true,
      firstname: true,
    },
  });

  const options = {
    keys: ['realname', 'firstname', 'registrationNumber'],
  };

  const found = fuzzysort.go(q.trim(), users, options)
    .map((result) => ({
      obj: result.obj,
      highlight: fuzzysort.highlight(result) // error here
    }));

  return found;
});

What did I do wrong ?

when using keys you're given a KeysResult, not a Result.
a KeysResult is an array of Results, 1 for each of your keys.

fuzzysort.highlight(result[0]) // realname
fuzzysort.highlight(result[1]) // firstname
fuzzysort.highlight(result[2]) // registrationNumber

Thank you it works well now.