{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/26051","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/26051/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/26051/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/26051/events","html_url":"https://github.com/Microsoft/TypeScript/issues/26051","id":345561299,"node_id":"MDU6SXNzdWUzNDU1NjEyOTk=","number":26051,"title":"[Feature request] Stricter keyword than `extends` for generics restricting excessive keys. Maybe `is`? ","user":{"login":"joonhocho","id":930795,"node_id":"MDQ6VXNlcjkzMDc5NQ==","avatar_url":"https://avatars2.githubusercontent.com/u/930795?v=4","gravatar_id":"","url":"https://api.github.com/users/joonhocho","html_url":"https://github.com/joonhocho","followers_url":"https://api.github.com/users/joonhocho/followers","following_url":"https://api.github.com/users/joonhocho/following{/other_user}","gists_url":"https://api.github.com/users/joonhocho/gists{/gist_id}","starred_url":"https://api.github.com/users/joonhocho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joonhocho/subscriptions","organizations_url":"https://api.github.com/users/joonhocho/orgs","repos_url":"https://api.github.com/users/joonhocho/repos","events_url":"https://api.github.com/users/joonhocho/events{/privacy}","received_events_url":"https://api.github.com/users/joonhocho/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-07-29T20:44:20Z","updated_at":"2018-07-30T01:55:25Z","closed_at":null,"author_association":"NONE","body":"## Search Terms\r\n\r\nexact, generic, strict, keyword \r\n\r\n## Suggestion\r\n\r\nI wish there is a way to enforce exact shape (same keys: no excessive keys allowed) of generic type.\r\nnew keyword `is` maybe?\r\n\r\n## Use Cases\r\n\r\nEliminate cases where excessive keys are supplied to generic object parameter and cause uncaught bugs.\r\n\r\n## Examples\r\n\r\n```typescript\r\nconst mapProperties = <\r\n  T extends { [key: string]: any },\r\n  U extends { [K in keyof T]: (v: T[K]) => any }\r\n>(\r\n  obj: T,\r\n  mappers: U\r\n): { [K in keyof T]: ReturnType<U[K]> } => {\r\n  const res = {};\r\n  Object.keys(mappers).forEach((key) => {\r\n    res[key] = mappers[key](obj[key]);\r\n  });\r\n  return res;\r\n};\r\n\r\n// data to be transformed\r\nconst data = {\r\n  a: 1,\r\n  // b: 0, // used to exist but removed later\r\n};\r\n\r\n// in another file...\r\nconst mapper = {\r\n  a: String,\r\n  b: Boolean // not wanted anymore but forgot to remvoe\r\n}\r\n\r\nconst res = mapObject(data, mapper) // not throwing type error even if there is `b` in mapper and not in data.\r\n// res = {\r\n//   a: '1',\r\n//   b: false // unwanted bug\r\n// }\r\n\r\nBy using generics, res inherits return type from mapper \r\n```\r\n\r\n### With new keyword\r\n```typescript\r\nconst mapProperties = <\r\n  T extends { [key: string]: any },\r\n  U is { [K in keyof T]: (v: T[K]) => any } // enforces exact keys\r\n>(\r\n  obj: T,\r\n  mappers: U\r\n) ...\r\n\r\nmapObject({a: 1}, {a: String, b: Boolean}) // error because `b` does not exist in T.\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [ x ] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [ x ] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [ x ] This could be implemented without emitting different JS based on the types of the expressions\r\n* [ x ] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/408725446","html_url":"https://github.com/Microsoft/TypeScript/issues/26051#issuecomment-408725446","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/26051","id":408725446,"node_id":"MDEyOklzc3VlQ29tbWVudDQwODcyNTQ0Ng==","user":{"login":"joonhocho","id":930795,"node_id":"MDQ6VXNlcjkzMDc5NQ==","avatar_url":"https://avatars2.githubusercontent.com/u/930795?v=4","gravatar_id":"","url":"https://api.github.com/users/joonhocho","html_url":"https://github.com/joonhocho","followers_url":"https://api.github.com/users/joonhocho/followers","following_url":"https://api.github.com/users/joonhocho/following{/other_user}","gists_url":"https://api.github.com/users/joonhocho/gists{/gist_id}","starred_url":"https://api.github.com/users/joonhocho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joonhocho/subscriptions","organizations_url":"https://api.github.com/users/joonhocho/orgs","repos_url":"https://api.github.com/users/joonhocho/repos","events_url":"https://api.github.com/users/joonhocho/events{/privacy}","received_events_url":"https://api.github.com/users/joonhocho/received_events","type":"User","site_admin":false},"created_at":"2018-07-30T01:55:25Z","updated_at":"2018-07-30T01:55:25Z","author_association":"NONE","body":"@wkrueger I don't see how it can be applied to the above case. Could you explain?"}}
{"actor":{"display_login":"joonhocho"}}