Skip to content

problem with interfaces  #6175

Description

@jeevan-reddy

Error:
Argument of type '{ size: number; label: string; }' is not assignable to parameter of type '{ label: string; }'.
Object literal may only specify known properties, and 'size' does not exist in type '{ label: string; }'.

My typesscript version is 1.7.5

Steps to recreate the Error:

  1. Create a .ts file named script.ts
  2. type the following code :
function printLabel(labelledObj: {label: string}) {
  console.log(labelledObj.label);
}
var myObj = {size: 10, label: "Size 10 Object"};  // myObj is of type {size:number;label:string}
// printLabel(myObj);
printLabel({size: 10, label: "Size 10 Object"});   // the object being passed to printLabel is also of type {size:number;label:string}
  1. Compile the script.ts using command tsc script either from command prompt/powershell

it is clearly mentioned in documentation that if we send extra arguments it is ok .please refer to attached code-snippet
code-snippet

But what is the difference if i assign an object to a variable , pass that variable as argument and if i directly pass the object as argument ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions