{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7422","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7422/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7422/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7422/events","html_url":"https://github.com/Microsoft/TypeScript/pull/7422","id":139059611,"number":7422,"title":"Narrow type constraints","user":{"login":"sandersn","id":293473,"avatar_url":"https://avatars.githubusercontent.com/u/293473?v=3","gravatar_id":"","url":"https://api.github.com/users/sandersn","html_url":"https://github.com/sandersn","followers_url":"https://api.github.com/users/sandersn/followers","following_url":"https://api.github.com/users/sandersn/following{/other_user}","gists_url":"https://api.github.com/users/sandersn/gists{/gist_id}","starred_url":"https://api.github.com/users/sandersn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandersn/subscriptions","organizations_url":"https://api.github.com/users/sandersn/orgs","repos_url":"https://api.github.com/users/sandersn/repos","events_url":"https://api.github.com/users/sandersn/events{/privacy}","received_events_url":"https://api.github.com/users/sandersn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/cla-not-required","name":"cla-not-required","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-07T18:51:09Z","updated_at":"2016-03-08T23:51:06Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/7422","html_url":"https://github.com/Microsoft/TypeScript/pull/7422","diff_url":"https://github.com/Microsoft/TypeScript/pull/7422.diff","patch_url":"https://github.com/Microsoft/TypeScript/pull/7422.patch"},"body":"Fixes #6445\r\n\r\nIf the narrowed type is assignable to the type parameter's *constraint*, then narrow to a type parameter with the constraint of the narrowed type. In the example in the bug, \r\n\r\n```ts\r\nisDerived(x) && isDerived(y) && x.d == y.d;\r\n```\r\n\r\nAfter narrowing, `x` and `y` have the type `T extends Derived` instead of `T extends Base`."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/194028587","html_url":"https://github.com/Microsoft/TypeScript/pull/7422#issuecomment-194028587","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/7422","id":194028587,"user":{"login":"sandersn","id":293473,"avatar_url":"https://avatars.githubusercontent.com/u/293473?v=3","gravatar_id":"","url":"https://api.github.com/users/sandersn","html_url":"https://github.com/sandersn","followers_url":"https://api.github.com/users/sandersn/followers","following_url":"https://api.github.com/users/sandersn/following{/other_user}","gists_url":"https://api.github.com/users/sandersn/gists{/gist_id}","starred_url":"https://api.github.com/users/sandersn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandersn/subscriptions","organizations_url":"https://api.github.com/users/sandersn/orgs","repos_url":"https://api.github.com/users/sandersn/repos","events_url":"https://api.github.com/users/sandersn/events{/privacy}","received_events_url":"https://api.github.com/users/sandersn/received_events","type":"User","site_admin":false},"created_at":"2016-03-08T23:51:06Z","updated_at":"2016-03-08T23:51:06Z","body":"As discussed with @yuit, the reason I narrow to `T extends Derived` instead of `Derived` is that I wanted to enable to returning the narrowed type. But this still doesn't work. Observe `f` and `f2`:\r\n\r\n```ts\r\nfunction f<T extends Base>(x: T) {\r\n  return isDerived(x) && x; // f's return type appears to be `T extends Base`\r\n}\r\nfunction f2<T extends Base>(x: T): T {\r\n  return isDerived(x) && x;\r\n  // error: 'T' is not assignable 'T'; 'Derived' is not assignable to 'T'\r\n}\r\n```\r\n\r\nThis should work by analogy with the non-generic version:\r\n```ts\r\nfunction g(x: Base) { \r\n  return isDerived(x) && x; // g's return type is Derived\r\n}\r\n```"}}