Skip to content

Interfaces with all members optional is almost the same as 'any' #9841

Closed
@bman61

Description

@bman61

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)

Code

interface AllMembersOptional {
    first?: string;
    second?: number;
}

let a: AllMembersOptional = 7;
let b: AllMembersOptional = () => 7;

Expected behavior:
compiler error '7' is not type AllMembersOptional -- let a: AllMembersOptional = 7;
compiler error 'function: number' is not type AllMembersOptional -- let b: AllMembersOptional = () => 7;

Actual behavior:
no compiler errors or warnings and the following javascript is generated:

var a = 7;
var b = function () { return 7; };

Actual example
Referencing angular.d.ts and converting an angular directive

angular.module('test').directive('custom', function () {
  return {
    controller: 'testCtrl',
    template: '<div></div>,
    //...
  };
});

carelessly into a component

angular.module('test').component('custom', function () {
  return {
    controller: 'testCtrl',
    template: '<div></div>,
    //...
  };
});

generates no typescript compiler error messages and will fail at run-time because the options parameter needs to be an object

angular.module('test').component('custom', {
    controller: 'testCtrl',
    template: '<div></div>,
    //...
  }
);

options parameter cannot a function (or number or string etc).
Just about anything passed in for a parameter of an interface type with all optional parameters will be allowed by typescript

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions