Skip to content

Wrong key type for FuseSortFunctionMatch type #811

@ryangoree

Description

@ryangoree

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

The sortFn option's arguments are typed as FuseSortFunctionArg:

sortFn?: FuseSortFunction

Fuse/src/index.d.ts

Lines 182 to 192 in fac1673

export type FuseSortFunctionArg = {
idx: number
item: FuseSortFunctionItem
score: number
matches?: (FuseSortFunctionMatch | FuseSortFunctionMatchList)[]
}
export type FuseSortFunction = (
a: FuseSortFunctionArg,
b: FuseSortFunctionArg
) => number

The type uses a string for the matches?.[idx]?.key property:

Fuse/src/index.d.ts

Lines 159 to 164 in fac1673

export type FuseSortFunctionMatch = {
score: number
key: string
value: string
indices: ReadonlyArray<number>[]
}

However, the actual type is an object created with the createKey function:

export function createKey(key) {
let path = null
let id = null
let src = null
let weight = 1
let getFn = null
if (isString(key) || isArray(key)) {
src = key
path = createKeyPath(key)
id = createKeyId(key)
} else {
if (!hasOwn.call(key, 'name')) {
throw new Error(ErrorMsg.MISSING_KEY_PROPERTY('name'))
}
const name = key.name
src = name
if (hasOwn.call(key, 'weight')) {
weight = key.weight
if (weight <= 0) {
throw new Error(ErrorMsg.INVALID_KEY_WEIGHT_VALUE(name))
}
}
path = createKeyPath(name)
id = createKeyId(name)
getFn = key.getFn
}
return { path, id, weight, src, getFn }
}

The Fuse.js version where this bug is happening.

6.6.2

Is this a regression?

  • This is a regression bug

Which version did this behavior use to work in?

None

Steps To Reproduce

  1. Run the following and observe that the logged key is an object:
const fuse = new Fuse(
  [
    { name: "test 1", id: "123" },
    { name: "test 2", id: "456" },
  ],
  {
    keys: ["name", "id"],
    includeMatches: true,
    sortFn: (a, b) => {
      console.log("key:", a.matches?.[0]?.key);
      return a.score - b.score;
    },
  }
);

fuse.search("test");
key: {path: Array(1), id: 'name', weight: 1, src: 'name', getFn: null}

Expected behavior

The key property on FuseSortFunctionMatch should be typed as an object based on the return type of createKey.

Screenshots

No response

Additional context

Important

The version selector in this issue template only lets me select up to version 6.6.2, but this was observed in version 7.1.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions