diff --git a/tests/baselines/reference/strictPropertyInitialization.errors.txt b/tests/baselines/reference/strictPropertyInitialization.errors.txt new file mode 100644 index 00000000000..6d13ea3646c --- /dev/null +++ b/tests/baselines/reference/strictPropertyInitialization.errors.txt @@ -0,0 +1,90 @@ +tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(4,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. +tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(6,5): error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor. +tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(48,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. +tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(71,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. + + +==== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts (4 errors) ==== + // Properties with non-undefined types require initialization + + class C1 { + a: number; // Error + ~ +!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. + b: number | undefined; + c: number | null; // Error + ~ +!!! error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor. + d?: number; + } + + // No strict initialization checks in ambient contexts + + declare class C2 { + a: number; + b: number | undefined; + c: number | null; + d?: number; + } + + // No strict initialization checks for static members + + class C3 { + static a: number; + static b: number | undefined; + static c: number | null; + static d?: number; + } + + // Initializer satisfies strict initialization check + + class C4 { + a = 0; + b: number = 0; + c: string = "abc"; + } + + // Assignment in constructor satisfies strict initialization check + + class C5 { + a: number; + constructor() { + this.a = 0; + } + } + + // All code paths must contain assignment + + class C6 { + a: number; // Error + ~ +!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. + constructor(cond: boolean) { + if (cond) { + return; + } + this.a = 0; + } + } + + class C7 { + a: number; + constructor(cond: boolean) { + if (cond) { + this.a = 1; + return; + } + this.a = 0; + } + } + + // Properties with string literal names aren't checked + + class C8 { + a: number; // Error + ~ +!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor. + "b": number; + 0: number; + } + \ No newline at end of file diff --git a/tests/baselines/reference/strictPropertyInitialization.js b/tests/baselines/reference/strictPropertyInitialization.js new file mode 100644 index 00000000000..7379e5a157d --- /dev/null +++ b/tests/baselines/reference/strictPropertyInitialization.js @@ -0,0 +1,176 @@ +//// [strictPropertyInitialization.ts] +// Properties with non-undefined types require initialization + +class C1 { + a: number; // Error + b: number | undefined; + c: number | null; // Error + d?: number; +} + +// No strict initialization checks in ambient contexts + +declare class C2 { + a: number; + b: number | undefined; + c: number | null; + d?: number; +} + +// No strict initialization checks for static members + +class C3 { + static a: number; + static b: number | undefined; + static c: number | null; + static d?: number; +} + +// Initializer satisfies strict initialization check + +class C4 { + a = 0; + b: number = 0; + c: string = "abc"; +} + +// Assignment in constructor satisfies strict initialization check + +class C5 { + a: number; + constructor() { + this.a = 0; + } +} + +// All code paths must contain assignment + +class C6 { + a: number; // Error + constructor(cond: boolean) { + if (cond) { + return; + } + this.a = 0; + } +} + +class C7 { + a: number; + constructor(cond: boolean) { + if (cond) { + this.a = 1; + return; + } + this.a = 0; + } +} + +// Properties with string literal names aren't checked + +class C8 { + a: number; // Error + "b": number; + 0: number; +} + + +//// [strictPropertyInitialization.js] +"use strict"; +// Properties with non-undefined types require initialization +var C1 = /** @class */ (function () { + function C1() { + } + return C1; +}()); +// No strict initialization checks for static members +var C3 = /** @class */ (function () { + function C3() { + } + return C3; +}()); +// Initializer satisfies strict initialization check +var C4 = /** @class */ (function () { + function C4() { + this.a = 0; + this.b = 0; + this.c = "abc"; + } + return C4; +}()); +// Assignment in constructor satisfies strict initialization check +var C5 = /** @class */ (function () { + function C5() { + this.a = 0; + } + return C5; +}()); +// All code paths must contain assignment +var C6 = /** @class */ (function () { + function C6(cond) { + if (cond) { + return; + } + this.a = 0; + } + return C6; +}()); +var C7 = /** @class */ (function () { + function C7(cond) { + if (cond) { + this.a = 1; + return; + } + this.a = 0; + } + return C7; +}()); +// Properties with string literal names aren't checked +var C8 = /** @class */ (function () { + function C8() { + } + return C8; +}()); + + +//// [strictPropertyInitialization.d.ts] +declare class C1 { + a: number; + b: number | undefined; + c: number | null; + d?: number; +} +declare class C2 { + a: number; + b: number | undefined; + c: number | null; + d?: number; +} +declare class C3 { + static a: number; + static b: number | undefined; + static c: number | null; + static d?: number; +} +declare class C4 { + a: number; + b: number; + c: string; +} +declare class C5 { + a: number; + constructor(); +} +declare class C6 { + a: number; + constructor(cond: boolean); +} +declare class C7 { + a: number; + constructor(cond: boolean); +} +declare class C8 { + a: number; + "b": number; + 0: number; +} diff --git a/tests/baselines/reference/strictPropertyInitialization.symbols b/tests/baselines/reference/strictPropertyInitialization.symbols new file mode 100644 index 00000000000..54b365732c4 --- /dev/null +++ b/tests/baselines/reference/strictPropertyInitialization.symbols @@ -0,0 +1,147 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts === +// Properties with non-undefined types require initialization + +class C1 { +>C1 : Symbol(C1, Decl(strictPropertyInitialization.ts, 0, 0)) + + a: number; // Error +>a : Symbol(C1.a, Decl(strictPropertyInitialization.ts, 2, 10)) + + b: number | undefined; +>b : Symbol(C1.b, Decl(strictPropertyInitialization.ts, 3, 14)) + + c: number | null; // Error +>c : Symbol(C1.c, Decl(strictPropertyInitialization.ts, 4, 26)) + + d?: number; +>d : Symbol(C1.d, Decl(strictPropertyInitialization.ts, 5, 21)) +} + +// No strict initialization checks in ambient contexts + +declare class C2 { +>C2 : Symbol(C2, Decl(strictPropertyInitialization.ts, 7, 1)) + + a: number; +>a : Symbol(C2.a, Decl(strictPropertyInitialization.ts, 11, 18)) + + b: number | undefined; +>b : Symbol(C2.b, Decl(strictPropertyInitialization.ts, 12, 14)) + + c: number | null; +>c : Symbol(C2.c, Decl(strictPropertyInitialization.ts, 13, 26)) + + d?: number; +>d : Symbol(C2.d, Decl(strictPropertyInitialization.ts, 14, 21)) +} + +// No strict initialization checks for static members + +class C3 { +>C3 : Symbol(C3, Decl(strictPropertyInitialization.ts, 16, 1)) + + static a: number; +>a : Symbol(C3.a, Decl(strictPropertyInitialization.ts, 20, 10)) + + static b: number | undefined; +>b : Symbol(C3.b, Decl(strictPropertyInitialization.ts, 21, 21)) + + static c: number | null; +>c : Symbol(C3.c, Decl(strictPropertyInitialization.ts, 22, 33)) + + static d?: number; +>d : Symbol(C3.d, Decl(strictPropertyInitialization.ts, 23, 28)) +} + +// Initializer satisfies strict initialization check + +class C4 { +>C4 : Symbol(C4, Decl(strictPropertyInitialization.ts, 25, 1)) + + a = 0; +>a : Symbol(C4.a, Decl(strictPropertyInitialization.ts, 29, 10)) + + b: number = 0; +>b : Symbol(C4.b, Decl(strictPropertyInitialization.ts, 30, 10)) + + c: string = "abc"; +>c : Symbol(C4.c, Decl(strictPropertyInitialization.ts, 31, 18)) +} + +// Assignment in constructor satisfies strict initialization check + +class C5 { +>C5 : Symbol(C5, Decl(strictPropertyInitialization.ts, 33, 1)) + + a: number; +>a : Symbol(C5.a, Decl(strictPropertyInitialization.ts, 37, 10)) + + constructor() { + this.a = 0; +>this.a : Symbol(C5.a, Decl(strictPropertyInitialization.ts, 37, 10)) +>this : Symbol(C5, Decl(strictPropertyInitialization.ts, 33, 1)) +>a : Symbol(C5.a, Decl(strictPropertyInitialization.ts, 37, 10)) + } +} + +// All code paths must contain assignment + +class C6 { +>C6 : Symbol(C6, Decl(strictPropertyInitialization.ts, 42, 1)) + + a: number; // Error +>a : Symbol(C6.a, Decl(strictPropertyInitialization.ts, 46, 10)) + + constructor(cond: boolean) { +>cond : Symbol(cond, Decl(strictPropertyInitialization.ts, 48, 16)) + + if (cond) { +>cond : Symbol(cond, Decl(strictPropertyInitialization.ts, 48, 16)) + + return; + } + this.a = 0; +>this.a : Symbol(C6.a, Decl(strictPropertyInitialization.ts, 46, 10)) +>this : Symbol(C6, Decl(strictPropertyInitialization.ts, 42, 1)) +>a : Symbol(C6.a, Decl(strictPropertyInitialization.ts, 46, 10)) + } +} + +class C7 { +>C7 : Symbol(C7, Decl(strictPropertyInitialization.ts, 54, 1)) + + a: number; +>a : Symbol(C7.a, Decl(strictPropertyInitialization.ts, 56, 10)) + + constructor(cond: boolean) { +>cond : Symbol(cond, Decl(strictPropertyInitialization.ts, 58, 16)) + + if (cond) { +>cond : Symbol(cond, Decl(strictPropertyInitialization.ts, 58, 16)) + + this.a = 1; +>this.a : Symbol(C7.a, Decl(strictPropertyInitialization.ts, 56, 10)) +>this : Symbol(C7, Decl(strictPropertyInitialization.ts, 54, 1)) +>a : Symbol(C7.a, Decl(strictPropertyInitialization.ts, 56, 10)) + + return; + } + this.a = 0; +>this.a : Symbol(C7.a, Decl(strictPropertyInitialization.ts, 56, 10)) +>this : Symbol(C7, Decl(strictPropertyInitialization.ts, 54, 1)) +>a : Symbol(C7.a, Decl(strictPropertyInitialization.ts, 56, 10)) + } +} + +// Properties with string literal names aren't checked + +class C8 { +>C8 : Symbol(C8, Decl(strictPropertyInitialization.ts, 65, 1)) + + a: number; // Error +>a : Symbol(C8.a, Decl(strictPropertyInitialization.ts, 69, 10)) + + "b": number; + 0: number; +} + diff --git a/tests/baselines/reference/strictPropertyInitialization.types b/tests/baselines/reference/strictPropertyInitialization.types new file mode 100644 index 00000000000..6c1e35908a9 --- /dev/null +++ b/tests/baselines/reference/strictPropertyInitialization.types @@ -0,0 +1,161 @@ +=== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts === +// Properties with non-undefined types require initialization + +class C1 { +>C1 : C1 + + a: number; // Error +>a : number + + b: number | undefined; +>b : number | undefined + + c: number | null; // Error +>c : number | null +>null : null + + d?: number; +>d : number | undefined +} + +// No strict initialization checks in ambient contexts + +declare class C2 { +>C2 : C2 + + a: number; +>a : number + + b: number | undefined; +>b : number | undefined + + c: number | null; +>c : number | null +>null : null + + d?: number; +>d : number | undefined +} + +// No strict initialization checks for static members + +class C3 { +>C3 : C3 + + static a: number; +>a : number + + static b: number | undefined; +>b : number | undefined + + static c: number | null; +>c : number | null +>null : null + + static d?: number; +>d : number | undefined +} + +// Initializer satisfies strict initialization check + +class C4 { +>C4 : C4 + + a = 0; +>a : number +>0 : 0 + + b: number = 0; +>b : number +>0 : 0 + + c: string = "abc"; +>c : string +>"abc" : "abc" +} + +// Assignment in constructor satisfies strict initialization check + +class C5 { +>C5 : C5 + + a: number; +>a : number + + constructor() { + this.a = 0; +>this.a = 0 : 0 +>this.a : number +>this : this +>a : number +>0 : 0 + } +} + +// All code paths must contain assignment + +class C6 { +>C6 : C6 + + a: number; // Error +>a : number + + constructor(cond: boolean) { +>cond : boolean + + if (cond) { +>cond : boolean + + return; + } + this.a = 0; +>this.a = 0 : 0 +>this.a : number +>this : this +>a : number +>0 : 0 + } +} + +class C7 { +>C7 : C7 + + a: number; +>a : number + + constructor(cond: boolean) { +>cond : boolean + + if (cond) { +>cond : boolean + + this.a = 1; +>this.a = 1 : 1 +>this.a : number +>this : this +>a : number +>1 : 1 + + return; + } + this.a = 0; +>this.a = 0 : 0 +>this.a : number +>this : this +>a : number +>0 : 0 + } +} + +// Properties with string literal names aren't checked + +class C8 { +>C8 : C8 + + a: number; // Error +>a : number + + "b": number; + 0: number; +} +