규칙 6.10
6장, 선언과 리소스 테스트
A local const binds its name once and leaves what it holds as writable as TypeScript does.
A write into it (to a field, a component or an element, or by a method that writes its object) must change the value when the initializer builds one nothing else holds (new, an object literal, an array literal or a type’s constructor, alone or with methods called on it), the declaration being emitted as a var from then on; it must be a write to that place when the initializer is a place: a name, a field or an element of one, or a call of a function of the file whose every return names one parameter (Rule 8.8, rule 1).
A const initialized from a place is a second name of that place, and nothing is emitted for it (Rule 8.8, change 0142).
A const initialized from any other call that may give an object that has a name is refused with the remedy, as TS8099, where the function writes an object after the declaration (Rule 8.8, rule 4), unless the program never writes an object of that type (Rule 8.8, rule 6).
A const nothing writes into stays WGSL’s let.
v, or a field or an element of v, is such a write when it is handed to a parameter that its function writes (Rule 8.25).
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
const v = new V(); v.bump() is how TypeScript is ordinarily written, since a const fixes the name and not the object; nothing else holds the object new built, so the local is that object, and a write through it changes what TypeScript changes.
A const initialized from a field or an element holds an object something else holds: TypeScript’s write reaches the object both hold, so the const is a second name of that place and the write reaches the place (change 0142, Amendment A3, rule 1).
A const initialized from a call that may give an object that has a name cannot be that place, and a copy would not show a write, so the front end refuses the declaration where the function writes an object after it, as not supported yet (change 0142, stage 2), and names the remedy.
Before this rule every write through a const was refused, with let as the remedy.
An object has the identity a JavaScript object has (Rule 8.8), so const w = v and let w = v make w a second name of v, always and not only where a write follows; Rule 7.2’s table records both.
출처
ECMAScript let and const declarations (a const binding cannot be assigned again, and what it holds is not frozen); Value Declarations and var Declarations; Rule 8.10; surface §26.
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
TS8099 for a const that takes a call’s result where the function writes an object after it ("c" would name an object that "pick(a, b)" gives back, which may already have a name, and the function writes an object after that. One object under two names is not supported yet (change 0142, stage 2). Read what you need from the call first, or write through the name that holds the object., src/compiler/ts/lower/second-name.ts), which also says a write through that const once, so that the write adds no second diagnostic; TS8005 CONST_ASSIGN for a write to a component of a const vector or matrix, which is a value ("v" is a const, and a write to a component of it changes it. Declare it with let to write it.), TS8035 CLASS_MEMBER for a method that writes its object called on a const that holds a copy; pinned by src/compiler/ts/member-assign.test.ts, src/compiler/ts/class-methods.test.ts and src/compiler/ts/class-syntax.test.ts, which also holds the three CPU paths to one value for a write through a const that built its value, and by src/compiler/ts/object-reference.test.ts and src/compiler/ts/second-name.test.ts for the second name; examples/gallery/class-parts/class-parts.shade.ts in the compile gate.
커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
-
examples/shade-examples.test.ts:26 -
src/compiler/ts/class-methods.test.ts:1 -
src/compiler/ts/class-syntax.test.ts:1550 -
src/compiler/ts/lower/second-name.ts:1 -
src/compiler/ts/member-assign.test.ts:1 -
src/compiler/ts/object-reference-removed-forms.test.ts:1 -
src/compiler/ts/object-reference-reviews.test.ts:1 -
src/compiler/ts/object-reference-unread.test.ts:1 -
src/compiler/ts/object-reference.test.ts:1 -
src/compiler/ts/reference-parameters.test.ts:1 -
src/compiler/ts/second-name-copies.test.ts:1 -
src/compiler/ts/second-name.test.ts:1
설명하는 절
커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.
적용하는 오류 코드
규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.
-
TS8005CONST_ASSIGN - 바뀔 수 없는 이름이나 자리에 값을 대입합니다.
const에 담은 수나 벡터,const벡터나 행렬의 성분,readonly필드나 static, 그리고readonly나"read"로 쓰지 않겠다고 약속한 스토리지 바인딩에는 값을 쓸 수 없습니다.const객체의 필드나 원소에 쓰는 것은 여기에 해당하지 않습니다.const는 객체를 담고 있을 뿐이고, 객체는 JavaScript에서처럼 계속 쓸 수 있기 때문입니다(규칙 6.10). -
TS8035CLASS_MEMBER - 표면이 받지 않는 클래스 멤버 모양, 클래스 규칙이 거부하는 멤버 사용, 클래스를 만들지 않는
new입니다(규칙 8.13). getter와 setter, static 필드와 메서드, 오버로드 시그니처, abstract 멤버,#비공개 이름, 객체를 바꾸는 메서드는 모두 컴파일됩니다. 메시지가 멤버나 대상과 고칠 방법을 알려 줍니다. -
TS8099UNSUPPORTED - 자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.
함께 보기
소스
커밋 af54ddad5의 규칙 원문:
docs/language-design.md:586(설계 문서)reqs/rules/RULE-0610.md(추적 항목)