简体   繁体   English

TypeScript 预期为 1 arguments,但得到 2

[英]TypeScript Expected 1 arguments, but got 2

What's wrong with my function createUser()?我的 function createUser() 有什么问题? Why I can't put params in Smoke.ts?为什么我不能在 Smoke.ts 中放入参数?

Login.ts:登录.ts:

interface User {
  url: string,
  email: string,
}

class Test{ 
async createUser(user: User) {
    await Page.setUrl(user.url);
    await Page.setEmail(user.email);


   
  }
}

Smoke.ts烟雾.ts

test("Smoke Test", async (t) => {
  console.log("Starting test");  
  await Login.createUser(
  "google.com","joe"
  );

An error appear: Expected 1 arguments, but got 2.出现错误:预期为 1 arguments,但得到 2。

The method createUser is expecting an object with the following shape: { url: string, email: string, }方法 createUser 期望 object 具有以下形状: { url: string, email: string, }

And you are passing a string as first parameter and another string as the second parameter.您将一个字符串作为第一个参数传递,另一个字符串作为第二个参数传递。

you should be passing an abject like this:你应该像这样传递一个卑鄙的人:

createUser({ 
   url: 'google.com', 
   email: 'joe' 
})

BTW why are you using "interface" and not "type" here?顺便说一句,你为什么在这里使用“界面”而不是“类型”? type is more common for defining object shapes and interface is often used to describe behaviours type 更常见于定义 object 形状和 interface 通常用于描述行为

your createUser function was declared with only one parameter but when you call this method you passed two-parameter.您的 createUser function 仅声明了一个参数,但是当您调用此方法时,您传递了两个参数。 to fix this you need to pass user object要解决此问题,您需要通过用户 object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM