Fix strictPropertyInitialization check to use effective value and add tests

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-11 17:00:26 +00:00
parent 014e792732
commit f8453ccb73
8 changed files with 113 additions and 1 deletions

View File

@@ -4070,7 +4070,7 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
}
function verifyCompilerOptions() {
if (options.strictPropertyInitialization && !getStrictOptionValue(options, "strictNullChecks")) {
if (getStrictOptionValue(options, "strictPropertyInitialization") && !getStrictOptionValue(options, "strictNullChecks")) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictPropertyInitialization", "strictNullChecks");
}
if (options.exactOptionalPropertyTypes && !getStrictOptionValue(options, "strictNullChecks")) {

View File

@@ -0,0 +1,18 @@
//// [tests/cases/compiler/exactOptionalPropertyTypesWithDefaultStrict.ts] ////
=== exactOptionalPropertyTypesWithDefaultStrict.ts ===
// When strict is not specified, it defaults to true in TS 6.0.
// strictNullChecks is effectively true, so exactOptionalPropertyTypes
// should work without producing TS5052.
interface Foo {
>Foo : Symbol(Foo, Decl(exactOptionalPropertyTypesWithDefaultStrict.ts, 0, 0))
bar?: string;
>bar : Symbol(Foo.bar, Decl(exactOptionalPropertyTypesWithDefaultStrict.ts, 4, 15))
}
const foo: Foo = {};
>foo : Symbol(foo, Decl(exactOptionalPropertyTypesWithDefaultStrict.ts, 8, 5))
>Foo : Symbol(Foo, Decl(exactOptionalPropertyTypesWithDefaultStrict.ts, 0, 0))

View File

@@ -0,0 +1,19 @@
//// [tests/cases/compiler/exactOptionalPropertyTypesWithDefaultStrict.ts] ////
=== exactOptionalPropertyTypesWithDefaultStrict.ts ===
// When strict is not specified, it defaults to true in TS 6.0.
// strictNullChecks is effectively true, so exactOptionalPropertyTypes
// should work without producing TS5052.
interface Foo {
bar?: string;
>bar : string | undefined
> : ^^^^^^^^^^^^^^^^^^
}
const foo: Foo = {};
>foo : Foo
> : ^^^
>{} : {}
> : ^^

View File

@@ -0,0 +1,15 @@
error TS5052: Option 'strictPropertyInitialization' cannot be specified without specifying option 'strictNullChecks'.
!!! error TS5052: Option 'strictPropertyInitialization' cannot be specified without specifying option 'strictNullChecks'.
==== strictPropertyInitializationDefaultStrictNullChecks.ts (0 errors) ====
// When strict is not specified, it defaults to true in TS 6.0.
// strictPropertyInitialization is effectively true via the strict default.
// Specifying strictNullChecks: false should produce an error because
// strictPropertyInitialization requires strictNullChecks.
class C {
x: number;
constructor() {}
}

View File

@@ -0,0 +1,17 @@
//// [tests/cases/compiler/strictPropertyInitializationDefaultStrictNullChecks.ts] ////
=== strictPropertyInitializationDefaultStrictNullChecks.ts ===
// When strict is not specified, it defaults to true in TS 6.0.
// strictPropertyInitialization is effectively true via the strict default.
// Specifying strictNullChecks: false should produce an error because
// strictPropertyInitialization requires strictNullChecks.
class C {
>C : Symbol(C, Decl(strictPropertyInitializationDefaultStrictNullChecks.ts, 0, 0))
x: number;
>x : Symbol(C.x, Decl(strictPropertyInitializationDefaultStrictNullChecks.ts, 5, 9))
constructor() {}
}

View File

@@ -0,0 +1,19 @@
//// [tests/cases/compiler/strictPropertyInitializationDefaultStrictNullChecks.ts] ////
=== strictPropertyInitializationDefaultStrictNullChecks.ts ===
// When strict is not specified, it defaults to true in TS 6.0.
// strictPropertyInitialization is effectively true via the strict default.
// Specifying strictNullChecks: false should produce an error because
// strictPropertyInitialization requires strictNullChecks.
class C {
>C : C
> : ^
x: number;
>x : number
> : ^^^^^^
constructor() {}
}

View File

@@ -0,0 +1,12 @@
// @exactOptionalPropertyTypes: true
// @noEmit: true
// When strict is not specified, it defaults to true in TS 6.0.
// strictNullChecks is effectively true, so exactOptionalPropertyTypes
// should work without producing TS5052.
interface Foo {
bar?: string;
}
const foo: Foo = {};

View File

@@ -0,0 +1,12 @@
// @strictNullChecks: false
// @noEmit: true
// When strict is not specified, it defaults to true in TS 6.0.
// strictPropertyInitialization is effectively true via the strict default.
// Specifying strictNullChecks: false should produce an error because
// strictPropertyInitialization requires strictNullChecks.
class C {
x: number;
constructor() {}
}