{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/2175","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/2175/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/2175/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/2175/events","html_url":"https://github.com/Microsoft/TypeScript/issues/2175","id":59392178,"number":2175,"title":"Default generic type variables","user":{"login":"andrewvarga","id":911440,"avatar_url":"https://avatars.githubusercontent.com/u/911440?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewvarga","html_url":"https://github.com/andrewvarga","followers_url":"https://api.github.com/users/andrewvarga/followers","following_url":"https://api.github.com/users/andrewvarga/following{/other_user}","gists_url":"https://api.github.com/users/andrewvarga/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewvarga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewvarga/subscriptions","organizations_url":"https://api.github.com/users/andrewvarga/orgs","repos_url":"https://api.github.com/users/andrewvarga/repos","events_url":"https://api.github.com/users/andrewvarga/events{/privacy}","received_events_url":"https://api.github.com/users/andrewvarga/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Needs%20Proposal","name":"Needs Proposal","color":"556b75"},{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Suggestion","name":"Suggestion","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-03-01T11:14:01Z","updated_at":"2015-08-24T22:56:03Z","closed_at":null,"body":"It would be really useful if I could set default values for my generic type variables. In many cases, the users of my class don't want to use a different value and so it's cumbersome to having to type it in.\r\n\r\nIf I could do this:\r\n```typescript\r\nclass MyClass<T = String>\r\n{\r\npublic userData: T;\r\nconstructor() {}\r\n}\r\n```\r\n\r\nthen the people who don't care about the type of `userData` (because they don't use it) and the people who are fine with it being a `String` don't have to set a generic type but others can if they want.\r\n\r\nAlso, these type variables could default to `any` or the type they extend (if they do) without explicitly defaulting them.\r\nFor example:\r\n` <T extends MyClass>` would default to `MyClass` (without explicitly writing it)\r\nand\r\n`<T>` could default to `any`\r\n\r\nWhat do you think?"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/134405086","html_url":"https://github.com/Microsoft/TypeScript/issues/2175#issuecomment-134405086","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/2175","id":134405086,"user":{"login":"mdekrey","id":144461,"avatar_url":"https://avatars.githubusercontent.com/u/144461?v=3","gravatar_id":"","url":"https://api.github.com/users/mdekrey","html_url":"https://github.com/mdekrey","followers_url":"https://api.github.com/users/mdekrey/followers","following_url":"https://api.github.com/users/mdekrey/following{/other_user}","gists_url":"https://api.github.com/users/mdekrey/gists{/gist_id}","starred_url":"https://api.github.com/users/mdekrey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdekrey/subscriptions","organizations_url":"https://api.github.com/users/mdekrey/orgs","repos_url":"https://api.github.com/users/mdekrey/repos","events_url":"https://api.github.com/users/mdekrey/events{/privacy}","received_events_url":"https://api.github.com/users/mdekrey/received_events","type":"User","site_admin":false},"created_at":"2015-08-24T22:56:03Z","updated_at":"2015-08-24T22:56:03Z","body":"Taking from @andrewvarga's example, I'd actually prefer that the default goes after the `extends` clause; `T` needs to be what `extends HTMLElement`, as we know that `HTMLDivElement` already does.\r\n\r\n    class ElementContainer<T extends HTMLElement = HTMLDivElement>\r\n    {\r\n       private _el: T;\r\n       constructor(el: T)\r\n       {\r\n          this._el = el;\r\n          el.style.color = \"#ff0000\";\r\n       }\r\n    }\r\n    // use:\r\n    var container: ElementContainer;\r\n\r\nAs such, I guess my proposal becomes to:\r\n\r\n    interface IMyBaseInterface { }\r\n\r\n    interface IMyInterface extends IMyBaseInterface { }\r\n\r\n    interface IMyOtherInterface extends IMyBaseInterface { }\r\n\r\n    class MyClass<T = string, U extends IMyBaseInterface = IMyInterface>\r\n    {\r\n        constructor(a: T) { }\r\n    }\r\n\r\n    // 'T' is a number, because it was inferred.\r\n    // 'U' is 'IMyInterface', because it was inferred as '{}' by existing logic.\r\n    var myVar = new MyClass(15);\r\n\r\n    // 'T' is a string, because it was inferred as 'any' by existing logic.\r\n    // 'U' is 'IMyInterface', because it was inferred as '{}' by existing logic.\r\n    var myVar2 = new MyClass(null);\r\n\r\n    // 'T' is a number, because it was specified.\r\n    // 'U' is 'IMyInterface', because it was inferred as '{}'.\r\n    var myVar3 = new MyClass<number>(null);\r\n\r\n    // 'T' is a number, because it was specified.\r\n    // 'U' is 'IMyOtherInterface', because it was specified.\r\n    var myVar4 = new MyClass<number, IMyOtherInterface>(null);\r\n\r\nIs there anything else needed to make a formal proposal?"}}