Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: Pass an extra parameter #6963

Closed
nirus opened this issue Feb 8, 2016 · 6 comments
Closed

Typescript: Pass an extra parameter #6963

nirus opened this issue Feb 8, 2016 · 6 comments
Assignees
Labels
Spec Issues related to the TypeScript language specification

Comments

@nirus
Copy link

nirus commented Feb 8, 2016

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

Same is been asked in stackoverflow:

I have attached the screenshot FYI.
capture

@yortus
Copy link
Contributor

yortus commented Feb 8, 2016

I'd say the documentation is wrong / out of date. The error you are seeing is an example of #3823. It would have worked before that PR, which landed in v1.6.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Docs The issue relates to how you learn TypeScript labels Feb 8, 2016
@DanielRosenwasser DanielRosenwasser added Spec Issues related to the TypeScript language specification and removed Docs The issue relates to how you learn TypeScript labels Feb 8, 2016
@DanielRosenwasser
Copy link
Member

This is in the spec, not the docs, just for tagging clarification.

@mhegazy mhegazy removed the Bug A bug in TypeScript label Feb 8, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Feb 8, 2016
@nirus
Copy link
Author

nirus commented Feb 9, 2016

As pointed by the Stackoverflow answer below code fixes the problem

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

This is not given in spec. Is this a feature or the implementation has been changed as a part of progression.

@weswigham
Copy link
Member

It's a cast - casting is in the spec, and it's a feature. Just remember that a cast is basically you overriding the compiler and saying "this is the type of this thing at this position", and so its mostly your responsibility to make sure it's valid to cast in a given situation.

@weswigham
Copy link
Member

A "nicer" way to "fix" the types and allow passing an argument with extra members would be modifying the signature of getX like so:

function getX<T extends Point>(p: T) {  
    return p.x;  
}

Though it may not be immediately obvious how this is any different, this alters inference to allow for any subtype of Point to be passed as an argument, bypassing the "strictness" check added in 1.6.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 10, 2016

for future references, more information about strict object literal checking can be found at https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#strict-object-literal-assignment-checking

@mhegazy mhegazy added the Help Wanted You can do this label Feb 20, 2016
@mhegazy mhegazy modified the milestone: TypeScript 2.0 Feb 20, 2016
@DanielRosenwasser DanielRosenwasser removed the Help Wanted You can do this label Mar 9, 2016
@nirus nirus closed this as completed Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Spec Issues related to the TypeScript language specification
Projects
None yet
Development

No branches or pull requests

8 participants