打字稿:传递一个额外的参数(Typescript: Pass an extra parameter)
我正在尝试文档中的以下代码
interface Point { x: number; y: number; } function getX(p: Point) { return p.x; } class CPoint { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } getX(new CPoint(0, 0)); // Ok, fields match getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok getX({ x: 0 }); // Error: supplied parameter does not match
根据代码评论说下面的行应该没问题。
getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok
但我得到的错误如下:
错误TS2345:类型'{x:number; y:数字; 颜色:字符串; }'不能分配给'Point'类型的参数。 对象文字只能指定已知属性,'Point'类型中不存在'color'
但下面的代码运行良好,我重写,其中我将params作为可选:
interface Point { x: number; y?: number; color?: string; } function getX(p: Point) { return p.x; } class CPoint { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } getX(new CPoint(0, 0)); // Ok, fields match getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok getX({ x: 0 }); // Error: supplied parameter does not match
如果文档错误或我在这里遗漏了什么,请有人帮助我
仅供参考我正在使用:
- 打字稿v1.7.5
- Visual Studio代码
截图:
I was trying the below code from the documentation
interface Point { x: number; y: number; } function getX(p: Point) { return p.x; } class CPoint { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } getX(new CPoint(0, 0)); // Ok, fields match getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok getX({ x: 0 }); // Error: supplied parameter does not match
As per the code comment says below line should be ok.
getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok
But i am getting error as below:
error TS2345: Argument of type '{ x: number; y: number; color: string; }' is not assignable to parameter of type 'Point'. Object literal may only specify known properties, and 'color' does not exist in type 'Point'
But the below code works well which i re-wrote in which i made params as optional:
interface Point { x: number; y?: number; color?: string; } function getX(p: Point) { return p.x; } class CPoint { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } getX(new CPoint(0, 0)); // Ok, fields match getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok getX({ x: 0 }); // Error: supplied parameter does not match
Please can somebody help me out if the documentation is wrong or am i missing something here
FYI i am using:
- Typescript v1.7.5
- Visual studio code
Screenshot:
原文:https://stackoverflow.com/questions/35267467
最满意答案
相关问答
更多-
你可以从这里开始, 用户层 MVC应用程序层 WCF服务层 SQL Server层... http://www.c-sharpcorner.com/UploadFile/d13d20/creating-aspnet-mvc-app-with-wcf-service-docx/ you can start here, User Tier MVC app Tier WCF Service Tier SQL Server Tier ... http://www.c-sharpcorner.com/UploadFi ...
-
你的网址有点不对劲。 这是我用的, protected String namespace = "http://www.nourhan-m.somee.com/"; protected String url = "http://www.nourhan-m.somee.com/WebService1.asmx"; protected String SOAPAction = "http://www.nourhan-m.somee.com/get_name"; protected String method_nam ...
-
无法连接到我的Web服务(Can't connect to my Web Service)[2023-02-03]
OMG ...为什么每个调用WebService devolper都犯了同样的错误 这是答案。 。 网络请求只能在ANDROID> 3.0的背景线程中进行 OMG...why same mistake is done by every devolper who is calling the WebService here is the answer.. NETWORK REQUEST CAN ONLY BE MADE ON BACKGROUND THREAD IN ANDROID >= 3.0 -
不,您不需要添加新路由(您已经可以连接到Web服务,因此所有路由都已存在)。 新服务可能会使您的工作更加困难,因为您需要配置3个不同应用程序之间的通信。 只需编辑您的Web应用程序,即可从您的Web服务获取数据。 No you don't need to add new route (you already can connect to web service so all routes already exists). New service may make your job even harder, ...
-
我想分享我的解决方案,因为这花费了我几个小时的头发撕裂。 事实证明,“Timed out”异常与代码无关,这是一个网络连接问题。 我用来调试应用程序的手机有时似乎连接到Wifi,即使它确实没有。 无论如何,如果您有此例外,请尝试检查您的网络连接。 祝你好运! I want to share my solution, as this has cost me hours of hair tearing. As it turns out, "Timed out" exception has nothing to ...
-
Web服务可以在IE中连接,但无法通过Visual Studio连接(Web service can connect in IE, but can't connect by Visual studio)[2022-07-03]
可能是您的企业环境将阻止Web服务。 你能试试以下吗? 添加防火墙出站规则以允许访问Web服务 在您的防病毒软件中添加Web服务作为可信URL(或者暂停防病毒保护以确定问题原因) 在您的IE中添加域名/ IP地址为可信任的 May be your corporate environment will block the Web Service. Can you please try the following? Add the Firewall outbound rule to allow to acces ... -
我没有尝试过Connectify ,但虚拟路由器为我工作。 如果您需要,还有更多说明。 I haven't tried Connectify, but Virtual Router worked for me. There are some more instructions here if you need it.
-
只需添加一个服务引用到http://server/servicefolder/service.svc 添加引用后,初始化客户端代理,例如 var client = new YourServiceName.YourServiceNameClient(); // Look for the client object. client.YourClientMethod(...); // Call the method defined in the client. Just add a service refe ...
-
您需要更改基址以指定“https”而不是“http”。 You need to change your base address to specify "https" rather than "http".
-
连接到休息Web服务(Connection to rest web service)[2023-08-31]
为了实现我使用Spring oauth2。 My Rest Api是资源服务器,客户端Api是oauth2客户端。 希望会有所帮助 To implement that I use Spring oauth2. My Rest Api is the resource server and the client Api is oauth2 client. Hope that will help