Skip to content

show error when use difference two way call function. #8682

Closed
@captainhamid

Description

@captainhamid

I use typescript version 1.8 (but this problem, there was Previous version and now version)
problem: show error when use difference two way call function.

interface SquareConfig {
  color?: string;
  width?: number;
}

function createSquare(config: SquareConfig): {color: string; area: number} {
  var newSquare = {color: "white", area: 100};
  if (config.color) {
    newSquare.color = config.color;
  }
  if (config.width) {
    newSquare.area = config.width * config.width;
  }
  return newSquare;
}

two way call function and to reply
1.

var obj = {color1: "black"};
var mySquare = createSquare(obj);

is correct work
but
2.

var mySquare = createSquare({color1: "black"});

above code produce error.

error TS2345: Argument of type '{ color1: string; }' is not assignable to parameter of type 'SquareConfig'.
Object literal may only specify known properties, and 'color1' does not exist in type 'SquareConfig'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions