You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Amplify Gen2配置name为必填用户属性时出现类型错误

解决Amplify Auth设置name必填属性的TypeScript错误

错误原因

Amplify v2的StandardAttributes类型定义中未包含name字段,它将用户姓名拆分为givenName(名)和familyName(姓)两个标准属性。直接使用name会触发TypeScript类型校验失败,进而导致npx amplify sandbox命令执行报错。

解决方案

方案一:使用官方标准属性字段

将代码中的name替换为givenName和familyName,按需设置必填项:

import {defineAuth} from '@aws-amplify/backend';

export const auth = defineAuth({
    loginWith: {
        email: true
    },
    userAttributes: {
        givenName: {
            required: true,
            mutable: true
        },
        familyName: {
            required: true,
            mutable: true
        }
    }
});

方案二:自定义name属性

若必须使用name作为属性名,可将其定义为自定义用户属性(格式为custom:name),同时可选扩展TypeScript类型以消除警告:

import {defineAuth, type AuthResource} from '@aws-amplify/backend';

// 扩展类型支持自定义name属性
type ExtendedAuthResource = AuthResource & {
  userAttributes: {
    'custom:name': {
      required: boolean;
      mutable: boolean;
    };
  };
};

export const auth = defineAuth({
    loginWith: {
        email: true
    },
    userAttributes: {
        'custom:name': {
            required: true,
            mutable: true
        }
    }
} as ExtendedAuthResource);

修改完成后重新执行npx amplify sandbox即可通过TypeScript校验。

内容的提问来源于stack exchange,提问作者O. Durand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:42:16