Skip to content

useGenericAuth extractScopes type declaration is incorrect #2573

Open
@SebAtRenault

Description

@SebAtRenault

I try to pass a custom extractScopes function looking like this:

useGenericAuth({
/.../
       extractScopes: (user) => user?.roles?.map((role) => `read:${role}`) ?? [],
}),

The first '?' triggers the linter rule: eslint@typescript-eslint/no-unnecessary-condition, which makes sense when I look at the type of extractScopes. UserType extends {} so there's no way it can be null.
Then I checked the source for the default extractScopes implementation, and the first thing it does is check for null:

export function defaultExtractScopes<UserType>(user: UserType): string[] {
  if (user != null && typeof user === 'object' && 'scope' in user) {
    if (typeof user.scope === 'string') {
      return user.scope.split(' ');
    }
    if (Array.isArray(user.scope)) {
      return user.scope;
    }
  }
  return [];
}

Am I missing somehting? (probably). How can you use extractScopes in TS?

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions