Validating user claims with TypeScript

I’m looking for recommendations on improving the type safety in our Next.js application.

The default session has a user object which is typed as Claims, which is [key: string]: any. When using the eslint rule @typescript-eslint/no-unsafe-assignment, assignments throw errors such as:

import { getSession } from '@auth0/nextjs-auth0';

const session = await getSession(req, res);
const userId = session.user.sub
//        ^ Unsafe assignment of an `any` value

This is a pattern that is useful in both getServerSideProps and api methods, and using a type guard every time we need to access a property on the user object feels wrong.

Is there a better way to achieve this? Alternatively, is it possible to override the getSession method in our own instance of auth0 initialised with initAuth0?

1 Like

Same question… Any help?