aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

A type error occurs in data model crud. #13523

Closed rnrnstar2 closed 4 months ago

rnrnstar2 commented 5 months ago

Environment information

System:
  OS: macOS 14.0
  CPU: (10) arm64 Apple M2 Pro
  Memory: 146.84 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.5.0 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 9.8.0 - /usr/local/bin/npm
  pnpm: 8.15.5 - ~/Library/pnpm/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.3
  @aws-amplify/backend-cli: 1.0.4
  aws-amplify: 6.3.6
  aws-cdk: 2.144.0
  aws-cdk-lib: 2.144.0
  typescript: 5.4.5
AWS environment variables:
  AWS_DEFAULT_PROFILE = cloudteam
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1

Description

Here is the error message.

Type 'string' cannot be assigned to type 'string[]'. ts(2322)
util.d.ts(15, 130): Expected type is '{ [x: string]: string[]; [x: number]: string[]; id?: string; }' It is taken from the property 'name' declared here

スクリーンショット 2024-06-07 13 34 41

const schema = a
  .schema({
    Platform: a.enum(platformList),
    Category: a
      .model({
        name: a.string().required(),
        platform: a.ref("Platform").required(),
        content: a.string(),
        s3ImgKey: a.string(),
      })
      .secondaryIndexes((index) => [
        index("platform").queryField("listByPlatform"),
      ])
  })
  .authorization((allow) => [
    allow.publicApiKey(),
    allow.resource(deletePostHandler),
    allow.resource(schedulePostHandler),
  ]);
"name": "@aws-amplify/data-schema-types",
  "version": "1.0.1",
(property) create: (model: {
    [x: string]: string[];
    [x: number]: string[];
    id?: string;
}, options?: {
    authMode?: AuthMode;
    authToken?: string;
    headers?: CustomHeaders;
}) => SingularReturnValue<{
    [x: string]: string[];
    platform: Platform;
    ... 5 more ...;
    readonly updatedAt: string;
}>
ykethan commented 5 months ago

Hey👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

chrisbonifacio commented 4 months ago

Hi @rnrnstar2, can you please run this command to update your @aws-amplify/data-schema package?

npm update @aws-amplify/data-schema

Then, try your code again and let us know if the issue is resolved

rnrnstar2 commented 4 months ago

The error still occurs. Should I install @aws-amplify/data-schema by itself rather than as a dependency? Do you have a reference package.json?

chrisbonifacio commented 4 months ago

I'm not able to reproduce this issue on the latest versions of the amplify backend packages. Could also try updating aws-amplify, though I'm not sure if that will help with types.

Might be best to start clean by deleting node_modules, package-lock.json and then reinstalling dependencies. Also, might help to restart the TypeScript server in your IDE.

My dev dependencies are:

  "devDependencies": {
    "@aws-amplify/backend": "^1.0.3",
    "@aws-amplify/backend-cli": "^1.0.4",
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "aws-cdk": "^2.137.0",
    "aws-cdk-lib": "^2.137.0",
    "constructs": "^10.3.0",
    "eslint": "^8",
    "eslint-config-next": "14.1.0",
    "postcss": "^8",
    "tailwindcss": "^3.3.0",
    "typescript": "^5.3.3"
  }
rnrnstar2 commented 4 months ago

I deleted node_modules, cleaned package-lock.json and installed but it didn't improve anything. I ran TypeScript: Restart TS Server but it didn't solve the problem. Is it because I'm using cursor as the editor?

My dev dependencies are:

"devDependencies": {
    "@aws-amplify/backend": "^1.0.3",
    "@aws-amplify/backend-cli": "^1.0.4",
    "@tailwindcss/aspect-ratio": "^0.4.2",
    "@tailwindcss/typography": "^0.5.13",
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.4.19",
    "aws-cdk": "^2.144.0",
    "aws-cdk-lib": "^2.144.0",
    "constructs": "^10.3.0",
    "esbuild": "^0.21.4",
    "eslint": "^8",
    "eslint-config-next": "14",
    "postcss": "^8",
    "tailwindcss": "^3.4.4",
    "tsx": "^4.13.0",
    "typescript": "^5.4.5"
  }

These are part of package-lock.json. Even if you do a clean install, the 1.0.0 version is still a dependency.

スクリーンショット 2024-06-12 11 34 46 スクリーンショット 2024-06-12 11 35 02 スクリーンショット 2024-06-12 11 35 19

david-mcafee commented 4 months ago

@rnrnstar2 - I tried to reproduce this issue using your schema, the dependencies you've listed, and the Cursor IDE, but have not had any luck.

Looking at your schema, it appears that your mutation is missing the platform field, which is required in your schema. Here, the following:

    await client.models.Category.create({
        name: "test",
    });

should instead be:

    await client.models.Category.create({
        name: "test",
        platform: "TEST2", // unsure what your enum values are, as they are not listed in the schema
    });

When I attempt to exclude the platform field, I (correctly) receive the following error:

Argument of type '{ name: string; }' is not assignable to parameter of type '{ id?: string | undefined; name: string; platform: string; content?: Nullable<string> | undefined; s3ImgKey?: Nullable<string> | undefined; }'.
  Property 'platform' is missing in type '{ name: string; }' but required in type '{ id?: string | undefined; name: string; platform: string; content?: Nullable<string> | undefined; s3ImgKey?: Nullable<string> | undefined; }'.ts(2345)
resource.ts(17, 9): 'platform' is declared here.

Just as a check, have you run npx ampx sandbox after updating your schema?

chrisbonifacio commented 4 months ago

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

rpostulart commented 1 month ago

@chrisbonifacio

I have the same TS error on the schema when having set identifiers

amplify/data/resource.ts:62:18 - error TS2322: Type 'string' is not assignable to type 'never'.

62 .identifier(["eventID"])


amplify/data/resource.ts:83:18 - error TS2322: Type 'string' is not assignable to type 'never'.

83     .identifier(["profileID"])

amplify/data/resource.ts:96:18 - error TS2322: Type 'string' is not assignable to type 'never'.

96 .identifier(["eventID", "profileID"])


amplify/data/resource.ts:96:29 - error TS2322: Type 'string' is not assignable to type 'never'.

96     .identifier(["eventID", "profileID"])

amplify/data/resource.ts:108:18 - error TS2322: Type 'string' is not assignable to type 'never'.

108 .identifier(["messageID"])