{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/4372","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/4372/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/4372/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/4372/events","html_url":"https://github.com/Microsoft/TypeScript/issues/4372","id":102071041,"number":4372,"title":"Allow intersected numbers to be used in arithmetics & element access","user":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/In%20Discussion","name":"In Discussion","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":7,"created_at":"2015-08-20T06:23:38Z","updated_at":"2015-09-02T15:47:22Z","closed_at":null,"body":"I was experimenting with intersection types and primitives, and I discovered that when you create an intersection type with a number, you can't use it in arithmetics and element access:\r\n```typescript\r\ntype extendedNumber = number & { extended: any };\r\nlet a: extendedNumber;\r\nlet obj: any;\r\n\r\na + 1; // Operator '+' cannot be applied to types 'number & { extended: any }' and 'number'.\r\nobj[a]; // An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.\r\n```\r\nThough you can't create properties on numbers, this can be useful to add brands to numbers, like this:\r\n```typescript\r\ntype int = number & { __intBrand: void };\r\nfunction int(value: number) {\r\n  return <int> (value | 0);\r\n}\r\n```\r\nCurrently intersections are only allowed on these locations if all parts of the intersection are number or string, but that rule should only apply to union types. Suggestion: for intersection types (at least) one type should be number (or string) if it's used in arithmetics or element access.\r\n\r\nI've implemented a fix in #4373."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/137139733","html_url":"https://github.com/Microsoft/TypeScript/issues/4372#issuecomment-137139733","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/4372","id":137139733,"user":{"login":"jbondc","id":199519,"avatar_url":"https://avatars.githubusercontent.com/u/199519?v=3","gravatar_id":"","url":"https://api.github.com/users/jbondc","html_url":"https://github.com/jbondc","followers_url":"https://api.github.com/users/jbondc/followers","following_url":"https://api.github.com/users/jbondc/following{/other_user}","gists_url":"https://api.github.com/users/jbondc/gists{/gist_id}","starred_url":"https://api.github.com/users/jbondc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbondc/subscriptions","organizations_url":"https://api.github.com/users/jbondc/orgs","repos_url":"https://api.github.com/users/jbondc/repos","events_url":"https://api.github.com/users/jbondc/events{/privacy}","received_events_url":"https://api.github.com/users/jbondc/received_events","type":"User","site_admin":false},"created_at":"2015-09-02T15:47:22Z","updated_at":"2015-09-02T15:47:22Z","body":"Short term, would this not work?\r\n```\r\ntype int32 = number;\r\ntype int64 = number;\r\nlet i: int64 = 666;\r\n```\r\nGives a 'nominal' looking type system for numbers, enough to use the type names to emit types.\r\n\r\nSet types are a lot of work, but it would allow proper checking for things like:\r\n```\r\nlet num: number = 1.2;\r\nlet foo: int32 = num; // ? probably don't want this\r\n```\r\nThough haven't played around with intersection types.\r\n\r\n"}}