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

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

Closed
bman61 opened this issue Jul 20, 2016 · 4 comments
Closed

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

bman61 opened this issue Jul 20, 2016 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@bman61
Copy link

bman61 commented Jul 20, 2016

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

@sandersn
Copy link
Member

See this FAQ answer. An interface with all optional properties is similar to an empty interface in this case.

@sandersn sandersn added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 20, 2016
@bman61
Copy link
Author

bman61 commented Jul 20, 2016

It is similar to an empty (i.e. useless) interface but is it the same?

Per FAQ:
In general, you should never find yourself declaring an interface with no properties.

If never is a good idea, would it make sense to have a compiler flag to issue warnings/errors?

@sandersn
Copy link
Member

Take a look at #7485 and #3842 -- #7845 is an proposal to fix this based on code from the closed PR #3842.

@bman61
Copy link
Author

bman61 commented Jul 20, 2016

Looked at both. #3842 definitely discusses the issue and issues with implementing. this helps a lot. thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants