Typescript:传递一个额外的参数

Typescript:传递一个额外的参数,typescript,typescript1.7,Typescript,Typescript1.7,我正在尝试下面的代码 根据代码注释,下面的行应该可以 getX({ x: 0, y: 0, color: "red" }); // Extra fields Ok 但我得到的错误如下: interface Point { x: number; y?: number; color?: string; } function getX(p: Point) { return p.x; } class CPoint { x: numb

我正在尝试下面的代码

根据代码注释,下面的行应该可以

getX({ x: 0, y: 0, color: "red" });  // Extra fields Ok
但我得到的错误如下:

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
错误TS2345:类型为{x:number;y:number;color:string; }'不可分配给类型为'Point'的参数。对象字面化 只能指定已知属性,并且类型中不存在“颜色” “点”

但下面的代码运行良好,我重新编写了该代码,其中我将参数设置为可选参数:

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
如果文件有误或者我遗漏了什么,有人能帮我吗

供我参考:

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
  • Typescript v1.7.5
  • Visual studio代码
屏幕截图:

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

文件已过期。以前添加一个额外的属性是可以的,但是在TypeScript 1.6中它们是可以的

如果您想让它在TS 1.6+中工作,则必须执行类型断言:

getX({ x: 0, y: 0, color: "red" } as Point); // no error

文件已经过时了。以前添加一个额外的属性是可以的,但是在TypeScript 1.6中它们是可以的

如果您想让它在TS 1.6+中工作,则必须执行类型断言:

getX({ x: 0, y: 0, color: "red" } as Point); // no error

在屏幕上,您的代码按预期工作。这可能是editorI的一个问题,我刚刚用visual studio代码在我的计算机上试用过,没有任何错误。我有typescript版本1.7.5哦,是吗。。你能分享一下VS版本吗?我已经添加了screnshot。版本0.10.6,Shell 0.3435,节点4.1.1,你的代码正常工作。这可能是editorI的一个问题,我刚刚用visual studio代码在我的计算机上试用过,没有任何错误。我有typescript版本1.7.5哦,是吗。。你能分享VS版本吗?我已经添加了screnshot。版本0.10.6外壳0.3435节点4.1.1超级谢谢你+1。好吧,这必须要看文件。将帮助人们进入打字脚本。超级谢谢你+1。好吧,这必须要看文件。将帮助人们进入打字脚本。