{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5303","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5303/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5303/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5303/events","html_url":"https://github.com/Microsoft/TypeScript/issues/5303","id":111947394,"number":5303,"title":"Duck typing on anonymous object literals and class objects","user":{"login":"zeeshanhanif","id":1311782,"avatar_url":"https://avatars.githubusercontent.com/u/1311782?v=3","gravatar_id":"","url":"https://api.github.com/users/zeeshanhanif","html_url":"https://github.com/zeeshanhanif","followers_url":"https://api.github.com/users/zeeshanhanif/followers","following_url":"https://api.github.com/users/zeeshanhanif/following{/other_user}","gists_url":"https://api.github.com/users/zeeshanhanif/gists{/gist_id}","starred_url":"https://api.github.com/users/zeeshanhanif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zeeshanhanif/subscriptions","organizations_url":"https://api.github.com/users/zeeshanhanif/orgs","repos_url":"https://api.github.com/users/zeeshanhanif/repos","events_url":"https://api.github.com/users/zeeshanhanif/events{/privacy}","received_events_url":"https://api.github.com/users/zeeshanhanif/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-10-17T06:29:13Z","updated_at":"2015-10-17T08:08:49Z","closed_at":null,"body":"Based on new duck typing concept fresh objects can not have access property.\r\nLike this\r\n\r\n```javascript\r\nlet a = { name:\"Bill\"};\r\nlet b = {name:\"Bill\", age:20};\r\n\r\na = {name: \"James\", age:30 }; // Error - excess property not allowed\r\na = b; // No error - works fine\r\n```\r\n\r\nNow the questions is does this concept also applies to class objects\r\nLike this:\r\n\r\n```javascript\r\nclass Human {    \r\n    name: string;    \r\n    constructor(name: string){\r\n        this.name = name;\r\n    }\r\n}\r\n\r\nclass Animal {\r\n    name: string;\r\n    age: number;    \r\n    constructor(name: string, age: number){\r\n        this.name = name;\r\n        this.age = age;\r\n    }\r\n    eat(){\r\n        console.log(this.name + \" is a Human and is eating\");\r\n    }\r\n}\r\n\r\nlet h1 = new Human(\"James\");\r\nlet a1 = new Animal(\"Dog\", 2);\r\nh1 = new Animal(\"Cat\", 1); //this is working -- But based on duck typing concept it should not because it has excess property and its fresh object\r\n```\r\n\r\nThe error is same if I remove constructor.\r\n\r\nIs there any difference in duck typing concept for anonymous objects and class objects OR may be I'm missing something?\r\nOr is it really considered as fresh object?"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/148894708","html_url":"https://github.com/Microsoft/TypeScript/issues/5303#issuecomment-148894708","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5303","id":148894708,"user":{"login":"DanielRosenwasser","id":972891,"avatar_url":"https://avatars.githubusercontent.com/u/972891?v=3","gravatar_id":"","url":"https://api.github.com/users/DanielRosenwasser","html_url":"https://github.com/DanielRosenwasser","followers_url":"https://api.github.com/users/DanielRosenwasser/followers","following_url":"https://api.github.com/users/DanielRosenwasser/following{/other_user}","gists_url":"https://api.github.com/users/DanielRosenwasser/gists{/gist_id}","starred_url":"https://api.github.com/users/DanielRosenwasser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DanielRosenwasser/subscriptions","organizations_url":"https://api.github.com/users/DanielRosenwasser/orgs","repos_url":"https://api.github.com/users/DanielRosenwasser/repos","events_url":"https://api.github.com/users/DanielRosenwasser/events{/privacy}","received_events_url":"https://api.github.com/users/DanielRosenwasser/received_events","type":"User","site_admin":false},"created_at":"2015-10-17T08:08:49Z","updated_at":"2015-10-17T08:08:49Z","body":"*Object literals* have an associated freshness before they're bound to a variable. The same doesn't apply for any other expressions.\r\n\r\nThe idea is that object literals are often used for option bags (where the user has to manually type out each property name at each use-site), and this behavior catches typos.\r\n\r\nIf you'd like to prevent `Animal` from being assigned to a `Human`, you can add a `private` property to `Animal`, since `private` and `protected` properties need to originate from the same declaration to be compatible."}}