小编ber*_*t84的帖子

为什么类型“Record<string,unknown>”不接受具有已定义键的对象作为值

我试图在类型中拥有一个可以接受任何对象作为输入的函数。我把它定义为

type Props = { onSubmit: (data: Record<string, unknown>) => void};

Run Code Online (Sandbox Code Playgroud)

但是当我尝试将这种类型传递给它时

type SignIn = (data: {email: string, password: string}) => void;
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

Type 'SignIn' is not assignable to type '(data: Record<string, unknown>) => void'.
  Types of parameters 'data' and 'data' are incompatible.
    Type 'Record<string, unknown>' is missing the following properties from type '{ email: string; password: string; }': email, password
Run Code Online (Sandbox Code Playgroud)

错误的完整示例在这里

有趣的是这个作品

typescript

8
推荐指数
1
解决办法
8058
查看次数

标签 统计

typescript ×1