규칙 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 (a written number is an f32 by Rule 5.1, true and false a bool, new C() the struct C, and a type’s own constructor that type), and 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.

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

설명하는 절

커밋 c66579bf의 표면 문서에서 이 규칙을 설명하는 절입니다.

적용하는 오류 코드

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

TS8005 CONST_ASSIGN
const나 읽기 전용 리소스처럼 바뀔 수 없는 이름에 값을 대입합니다.
TS8010 STRUCT_FIELD
An invalid struct field or declaration shape.

함께 보기

소스

커밋 c66579bf의 규칙 원문:

이 페이지 편집 문제 보고