{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5917","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5917/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5917/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5917/events","html_url":"https://github.com/Microsoft/TypeScript/issues/5917","id":120284551,"number":5917,"title":"Trouble typing object whose values are of a particular type while maintaining the names of its keys","user":{"login":"rigdern","id":199935,"avatar_url":"https://avatars.githubusercontent.com/u/199935?v=3","gravatar_id":"","url":"https://api.github.com/users/rigdern","html_url":"https://github.com/rigdern","followers_url":"https://api.github.com/users/rigdern/followers","following_url":"https://api.github.com/users/rigdern/following{/other_user}","gists_url":"https://api.github.com/users/rigdern/gists{/gist_id}","starred_url":"https://api.github.com/users/rigdern/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rigdern/subscriptions","organizations_url":"https://api.github.com/users/rigdern/orgs","repos_url":"https://api.github.com/users/rigdern/repos","events_url":"https://api.github.com/users/rigdern/events{/privacy}","received_events_url":"https://api.github.com/users/rigdern/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Question","name":"Question","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-03T22:24:22Z","updated_at":"2015-12-03T23:23:11Z","closed_at":null,"body":"I'm trying to create an object and I want type checking to work in two different scenarios but I can't get both scenarios to pass the type checker at the same time.\r\n\r\nI have an object that I'm essentially treating as a set of key-value pairs. Each of these values is of the same type. Here are the two scenarios I want to achieve with this object:\r\n  1. When accessing properties on the object, I want the type checker to be aware of the particular keys on the object that have been defined.\r\n  2. I want to be able to pass the object to a function which accepts any object whose value are all of a particular type (it doesn't care about the names of the keys).\r\n\r\nHere's a complete example:\r\n\r\n```ts\r\nclass Person {\r\n\tconstructor(public age: number) {\r\n\t}\r\n}\r\n\r\n// If you use the commented out version of people, Goal 2 is achieved\r\n// but Goal 1 breaks.\r\n//var people: { [name: string]: Person } = {\r\nvar people = {\r\n\tjohn: new Person(10),\r\n\tpete: new Person(20),\r\n\ttim: new Person(30),\r\n\tsam: new Person(40),\r\n};\r\n\r\n// Goal 1: I want to get intellisense on people\r\nvar somePerson = people.john;\r\n\r\n// Goal 2: I want to have a function which accepts an object whose values are all\r\n// of type Person.\r\nfunction inspectPeople(people: { [name: string]: Person }) {\r\n}\r\ninspectPeople(people);\r\n```\r\n\r\nHere's a link to [this example in the TypeScript Playground](http://www.typescriptlang.org/Playground#src=class%20Person%20%7B%0A%09constructor(public%20age%3A%20number)%20%7B%0A%09%7D%0A%7D%0A%0A%2F%2F%20If%20you%20use%20the%20commented%20out%20version%20of%20people%2C%20Goal%202%20is%20achieved%0A%2F%2F%20but%20Goal%201%20breaks.%0A%2F%2Fvar%20people%3A%20%7B%20%5Bname%3A%20string%5D%3A%20Person%20%7D%20%3D%20%7B%0Avar%20people%20%3D%20%7B%0A%09john%3A%20new%20Person(10)%2C%0A%09pete%3A%20new%20Person(20)%2C%0A%09tim%3A%20new%20Person(30)%2C%0A%09sam%3A%20new%20Person(40)%2C%0A%7D%3B%0A%0A%2F%2F%20Goal%201%3A%20I%20want%20to%20get%20intellisense%20on%20people%0Avar%20somePerson%20%3D%20people.john%3B%0A%0A%2F%2F%20Goal%202%3A%20I%20want%20to%20have%20a%20function%20which%20accepts%20an%20object%20whose%20values%20are%20all%0A%2F%2F%20of%20type%20Person.%0Afunction%20inspectPeople(people%3A%20%7B%20%5Bname%3A%20string%5D%3A%20Person%20%7D)%20%7B%0A%7D%0AinspectPeople(people)%3B%0A)."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/161821784","html_url":"https://github.com/Microsoft/TypeScript/issues/5917#issuecomment-161821784","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/5917","id":161821784,"user":{"login":"rigdern","id":199935,"avatar_url":"https://avatars.githubusercontent.com/u/199935?v=3","gravatar_id":"","url":"https://api.github.com/users/rigdern","html_url":"https://github.com/rigdern","followers_url":"https://api.github.com/users/rigdern/followers","following_url":"https://api.github.com/users/rigdern/following{/other_user}","gists_url":"https://api.github.com/users/rigdern/gists{/gist_id}","starred_url":"https://api.github.com/users/rigdern/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rigdern/subscriptions","organizations_url":"https://api.github.com/users/rigdern/orgs","repos_url":"https://api.github.com/users/rigdern/repos","events_url":"https://api.github.com/users/rigdern/events{/privacy}","received_events_url":"https://api.github.com/users/rigdern/received_events","type":"User","site_admin":false},"created_at":"2015-12-03T23:23:11Z","updated_at":"2015-12-03T23:23:11Z","body":"Thanks for the suggestion! I was not aware of this solution and it does fix the type errors.\r\n\r\nHowever, I wish there was a less verbose/repetitive solution where something closer to my original definition of `people` would work:\r\n\r\n```ts\r\nvar people = {\r\n    john: new Person(10),\r\n    pete: new Person(20),\r\n    tim: new Person(30),\r\n    sam: new Person(40),\r\n};\r\n```\r\n\r\nHere are some more concise solutions I had in mind that don't seem to currently exist in TypeScript:\r\n  1. Allow the `{ [name: string]: Person }` signature of `inspectPeople` to accept an object like `people`.\r\n  2. Instead of changing the meaning of  `{ [name: string]: Person }`, introduce an alternative notation for expressing a type that would match any object whose properties are all of type `Person` (e.g. like `people`).\r\n  3. Allow developers to specify that, in addition to the key-value pairs defined, `people` supports an indexer so it can be passed to a function like `inpsectPeople`.\r\n\r\nIf there's no more concise way to express this scenario in the current version of TypeScript, are there thoughts on how it might be expressed in future versions or is the current solution considered to be good enough?"}}