규칙 8.14
이 페이지에서

규칙 8.14

8장, 함수와 진입점 테스트

A constructor’s parameter property (constructor(public x: f32), or private, protected or readonly in place of public) is a field of its class, at the constructor’s place among the members, which the constructor assigns from the parameter before the field initializers run. A field written without a type must take the type its initializer names (true and false a bool, new C() the struct C, a type’s own constructor that type, and a float-written number an f32); a field initialized by integer-written numbers must take its type from its uses by Rule 5.1, and an f32 where no use decides (change 0134). A field whose initializer names none, or that has neither, must be refused. A readonly field may be assigned only in a constructor of the class that declares it, which is TypeScript’s rule; readonly is shallow, as TypeScript’s is, so a write into what the field holds stands. Field initializers must run in TypeScript’s order: those of the classes above the class whose constructor runs first (in the base constructor its super(...) calls, or before its body when none of them has one), that class’s parameter properties and its own after super(...) returns, and those of the classes below it when its body returns, so an initializer a derived class writes for an inherited field runs last.

규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.

근거

all three are how a TypeScript class is ordinarily written; before this rule a field without a type was dropped from the struct with nothing said where it was declared and an unknown field at every use, and a readonly field took any write. Every initializer ran before the constructor’s body and a derived class’s one for an inherited field was dropped, so class B extends A { limit = 5. } built a B whose limit was A’s.

출처

TypeScript’s parameter properties and readonly modifier (TS2540); Rule 5.1; Rule 6.9; surface §26.

검증 방식

테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.

컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.

TS8010 STRUCT_FIELD for a field with no type, TS8005 CONST_ASSIGN for a write to a readonly field, pinned by src/compiler/ts/class-syntax.test.ts; a field of integer-written numbers in both halves by src/compiler/ts/use-inference-module.test.ts.

커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.

설명하는 절

커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.

적용하는 오류 코드

규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.

TS8005 CONST_ASSIGN
바뀔 수 없는 이름이나 자리에 값을 대입합니다. const에 담은 수나 벡터, const 벡터나 행렬의 성분, readonly 필드나 static, 그리고 readonly나 "read"로 쓰지 않겠다고 약속한 스토리지 바인딩에는 값을 쓸 수 없습니다. const 객체의 필드나 원소에 쓰는 것은 여기에 해당하지 않습니다. const는 객체를 담고 있을 뿐이고, 객체는 JavaScript에서처럼 계속 쓸 수 있기 때문입니다(규칙 6.10).
TS8010 STRUCT_FIELD
구조체 필드나 선언의 모양이 잘못되었습니다. 필드가 없는 클래스는 받아들입니다. 이 코드는 잘못된 필드 선언, 클래스가 아닌 빈 구조체, 적용되지 않는 필드의 @align, 사용처마다 location이 둘로 갈리는 사용자 정의 값을 다룹니다(규칙 6.6).

함께 보기

소스

커밋 af54ddad5의 규칙 원문:

이 페이지 편집 문제 보고