Skip to content

defineServerAuth widens literal types despite const generic #134

@onmax

Description

@onmax

Bug

defineServerAuth requires as const on string literals in additionalFields and generateId even though the generic uses const R. Without it, types like 'string' widen to string and fail to match FieldType.

Repro

// Works
export default defineServerAuth(() => ({
  advanced: { database: { generateId: 'uuid' as const } },
  user: {
    additionalFields: {
      address: { type: 'string' as const, required: false, fieldName: 'address' },
    },
  },
}))

// Fails — TS2769: Type 'string' is not assignable to type 'FieldType'
export default defineServerAuth(() => ({
  advanced: { database: { generateId: 'uuid' } },
  user: {
    additionalFields: {
      address: { type: 'string', required: false, fieldName: 'address' },
    },
  },
}))

Cause

The overload has <const R extends ServerAuthConfig> but ServerAuthConfig extends BetterAuthOptions which types deeply nested fields broadly (e.g. type: FieldType). For deeply nested properties the constraint wins over the const inference, so TypeScript widens the literal.

Suggestion

Narrow the generic to only infer the parts that matter (additionalFields, plugins, generateId) instead of constraining R against the full BetterAuthOptions. This way the const modifier can preserve literals without as const.

Version

@onmax/nuxt-better-auth v0.0.2-alpha.23

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