amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

Type error when using client.models.Todo.create

Open Zhuohui-Li opened this issue 9 months ago • 0 comments

Environment information

System:
  OS: macOS 14.4
  CPU: (10) arm64 Apple M1 Pro
  Memory: 147.61 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.12.2 - /private/var/folders/h8/0sklzj9n1pz2zjt34vl3t15w0000gn/T/xfs-18c58eee/node
  Yarn: 4.1.1 - /private/var/folders/h8/0sklzj9n1pz2zjt34vl3t15w0000gn/T/xfs-18c58eee/yarn
  npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/backend: 0.15.1
  @aws-amplify/backend-cli: 0.16.0
  aws-amplify: 6.1.4
  aws-cdk: 2.139.0
  aws-cdk-lib: 2.139.0
  typescript: 5.4.5
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

From document

// backend/amplify/data/resource.ts
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
const schema = a
  .schema({
    TimeStamp: a.customType({
      now: a.timestamp(),
      note: a.string(), // .default("test"), can't have default because The @default directive may only be added to object definitions annotated with @model
    }),
    PrivacySetting: a.enum(["PRIVATE", "FRIENDS_ONLY", "PUBLIC"]),
    Todo: a
      .model({
        content: a.string(),
        isDone: a.boolean().default(false),
        timeStamp: a.ref("TimeStamp"),
        privacySetting: a.ref("PrivacySetting"),
      })
      .authorization((allow) => [
        allow.custom(),
        allow.authenticated("identityPool"),
      ]),
  })
  .authorization((allow) => [allow.resource(todoListTestFunction)]);
// test-ui/src/todolist.tsx
import type { Schema } from "$/data/resource";
import { generateClient } from "aws-amplify/data";
const client = generateClient<Schema>({
  headers: {
    //...
  },
});

//...
export function ShowTodoList() {
  const [todos, setTodos] = useState<Schema["Todo"][]>([]);
 // works
 useEffect(() => {
    const sub = client.models.Todo.observeQuery().subscribe({
      next: ({ items }) => {
        setTodos([...items]);
      },
    });

    return () => sub.unsubscribe();
  }, []);
  const createTodo = async () => {
      await client.models.Todo.create(
        {
          // a type error here
          // Object literal may only specify known properties, and 'content' does not exist in type '{ id?: string | undefined; type: { readonly id: string; readonly createdAt: string; readonly updatedAt: string; content?: Nullable<string> | undefined; isDone?: Nullable<boolean> | undefined; timeStamp?: { ...; } | ... 1 more ... | undefined; privacySetting?: "PRIVATE" | ... 3 more ... | undefined; }; }'.ts(2353)
          // if I add `as any` it still works
          content: window.prompt("Todo content?"),
          isDone: false,
          timeStamp: {
            now: Date.now(),
            note: "test",
          },
        },
        {
          authToken: authToken,
          authMode: "lambda",
        },
      );
    };

Zhuohui-Li avatar May 01 '24 14:05 Zhuohui-Li