{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6532","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6532/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6532/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6532/events","html_url":"https://github.com/Microsoft/TypeScript/pull/6532","id":127352614,"number":6532,"title":"Readonly properties and index signatures","user":{"login":"ahejlsberg","id":4226954,"avatar_url":"https://avatars.githubusercontent.com/u/4226954?v=3","gravatar_id":"","url":"https://api.github.com/users/ahejlsberg","html_url":"https://github.com/ahejlsberg","followers_url":"https://api.github.com/users/ahejlsberg/followers","following_url":"https://api.github.com/users/ahejlsberg/following{/other_user}","gists_url":"https://api.github.com/users/ahejlsberg/gists{/gist_id}","starred_url":"https://api.github.com/users/ahejlsberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahejlsberg/subscriptions","organizations_url":"https://api.github.com/users/ahejlsberg/orgs","repos_url":"https://api.github.com/users/ahejlsberg/repos","events_url":"https://api.github.com/users/ahejlsberg/events{/privacy}","received_events_url":"https://api.github.com/users/ahejlsberg/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":{"login":"ahejlsberg","id":4226954,"avatar_url":"https://avatars.githubusercontent.com/u/4226954?v=3","gravatar_id":"","url":"https://api.github.com/users/ahejlsberg","html_url":"https://github.com/ahejlsberg","followers_url":"https://api.github.com/users/ahejlsberg/followers","following_url":"https://api.github.com/users/ahejlsberg/following{/other_user}","gists_url":"https://api.github.com/users/ahejlsberg/gists{/gist_id}","starred_url":"https://api.github.com/users/ahejlsberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahejlsberg/subscriptions","organizations_url":"https://api.github.com/users/ahejlsberg/orgs","repos_url":"https://api.github.com/users/ahejlsberg/repos","events_url":"https://api.github.com/users/ahejlsberg/events{/privacy}","received_events_url":"https://api.github.com/users/ahejlsberg/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/Microsoft/TypeScript/milestones/1","html_url":"https://github.com/Microsoft/TypeScript/milestones/TypeScript%202.0","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/milestones/1/labels","id":721040,"number":1,"title":"TypeScript 2.0","description":"The next major release of TypeScript","creator":{"login":"RyanCavanaugh","id":6685088,"avatar_url":"https://avatars.githubusercontent.com/u/6685088?v=3","gravatar_id":"","url":"https://api.github.com/users/RyanCavanaugh","html_url":"https://github.com/RyanCavanaugh","followers_url":"https://api.github.com/users/RyanCavanaugh/followers","following_url":"https://api.github.com/users/RyanCavanaugh/following{/other_user}","gists_url":"https://api.github.com/users/RyanCavanaugh/gists{/gist_id}","starred_url":"https://api.github.com/users/RyanCavanaugh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RyanCavanaugh/subscriptions","organizations_url":"https://api.github.com/users/RyanCavanaugh/orgs","repos_url":"https://api.github.com/users/RyanCavanaugh/repos","events_url":"https://api.github.com/users/RyanCavanaugh/events{/privacy}","received_events_url":"https://api.github.com/users/RyanCavanaugh/received_events","type":"User","site_admin":false},"open_issues":137,"closed_issues":14,"state":"open","created_at":"2014-07-14T23:24:03Z","updated_at":"2016-01-25T21:45:11Z","due_on":null,"closed_at":null},"comments":9,"created_at":"2016-01-19T02:41:56Z","updated_at":"2016-01-26T23:25:14Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/6532","html_url":"https://github.com/Microsoft/TypeScript/pull/6532","diff_url":"https://github.com/Microsoft/TypeScript/pull/6532.diff","patch_url":"https://github.com/Microsoft/TypeScript/pull/6532.patch"},"body":"This PR adds support for readonly properties and index signatures as suggested in #12. Changes include:\r\n\r\n* A new `readonly` modifier that can applied to property and index signature declarations.\r\n* A property or index signature declared with the `readonly` modifier is considered read-only.\r\n* Read-only properties may have initializers and may be assigned to in constructors within the same class declaration, but otherwise assignments to read-only properties are disallowed.\r\n* Assignments to read-only index signatures are always disallowed.\r\n\r\nEntities are *implicitly* read-only in several situations:\r\n\r\n* A property declared with a `get` accessor and no `set` accessor is considered read-only.\r\n* In the type of an enum object, enum members are considered read-only properties.\r\n* In the type of a module object, exported `const` variables are considered read-only properties.\r\n* An entity declared in an `import` statement is considered read-only.\r\n* An entity accessed through an ES2015 namespace import is considered read-only (e.g. `foo.x` is read-only when `foo` is declared as `import * as foo from \"foo\"`).\r\n\r\nSome examples:\r\n\r\n```typescript\r\ninterface Point {\r\n    readonly x: number;\r\n    readonly y: number;\r\n}\r\n\r\nvar p1: Point = { x: 10, y: 20 };\r\np1.x = 5;  // Error, p1.x is read-only\r\n\r\nvar p2 = { x: 1, y: 1 };\r\nvar p3: Point = p2;  // Ok, read-only alias for p2\r\np3.x = 5;  // Error, p3.x is read-only\r\np2.x = 5;  // Ok, but also changes p3.x because of aliasing\r\n```\r\n```typescript\r\nclass Foo {\r\n    readonly a = 1;\r\n    readonly b: string;\r\n    constructor() {\r\n        this.b = \"hello\";  // Assignment permitted in constructor\r\n    }\r\n}\r\n```\r\n```typescript\r\n// Read-only view of Array<T> with all mutating methods removed\r\ninterface ReadonlyArray<T> {\r\n    readonly [x: number]: T;\r\n    readonly length: number;\r\n    toString(): string;\r\n    toLocaleString(): string;\r\n    concat<U extends ReadonlyArray<T>>(...items: U[]): T[];\r\n    concat(...items: T[]): T[];\r\n    join(separator?: string): string;\r\n    slice(start?: number, end?: number): T[];\r\n    indexOf(searchElement: T, fromIndex?: number): number;\r\n    lastIndexOf(searchElement: T, fromIndex?: number): number;\r\n    every(callbackfn: (value: T, index: number) => boolean, thisArg?: any): boolean;\r\n    some(callbackfn: (value: T, index: number) => boolean, thisArg?: any): boolean;\r\n    forEach(callbackfn: (value: T, index: number) => void, thisArg?: any): void;\r\n    map<U>(callbackfn: (value: T, index: number) => U, thisArg?: any): U[];\r\n    filter(callbackfn: (value: T, index: number) => boolean, thisArg?: any): T[];\r\n    reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T, initialValue?: T): T;\r\n    reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number) => U, initialValue: U): U;\r\n    reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T, initialValue?: T): T;\r\n    reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number) => U, initialValue: U): U;\r\n}\r\n\r\nlet a: Array<number> = [0, 1, 2, 3, 4];\r\nlet b: ReadonlyArray<number> = a;\r\nb[5] = 5;      // Error, elements are read-only\r\nb.push(5);     // Error, no push method (because it mutates array)\r\nb.length = 3;  // Error, length is read-only\r\na = b;         // Error, mutating methods are missing\r\n```\r\n\r\nNote that the `readonly` modifier does not affect subtype and assignability relationships of the containing type for the [reasons described here](https://github.com/Microsoft/TypeScript/pull/6532#issuecomment-174356151). Also note that this PR specifically doesn't attempt to formalize immutable objects. However, with the `readonly` modifier it becomes a lot more convenient to implement such objects.\r\n\r\nThe PR still needs tests, but I thought I'd get it up there for folks to take a look at."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/175290862","html_url":"https://github.com/Microsoft/TypeScript/pull/6532#issuecomment-175290862","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/6532","id":175290862,"user":{"login":"ahejlsberg","id":4226954,"avatar_url":"https://avatars.githubusercontent.com/u/4226954?v=3","gravatar_id":"","url":"https://api.github.com/users/ahejlsberg","html_url":"https://github.com/ahejlsberg","followers_url":"https://api.github.com/users/ahejlsberg/followers","following_url":"https://api.github.com/users/ahejlsberg/following{/other_user}","gists_url":"https://api.github.com/users/ahejlsberg/gists{/gist_id}","starred_url":"https://api.github.com/users/ahejlsberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahejlsberg/subscriptions","organizations_url":"https://api.github.com/users/ahejlsberg/orgs","repos_url":"https://api.github.com/users/ahejlsberg/repos","events_url":"https://api.github.com/users/ahejlsberg/events{/privacy}","received_events_url":"https://api.github.com/users/ahejlsberg/received_events","type":"User","site_admin":false},"created_at":"2016-01-26T23:25:14Z","updated_at":"2016-01-26T23:25:14Z","body":"@simonbuchan Yes, idea is to automatically synthesize read-only views of arbitrary types."}}