mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-13 18:14:48 -05:00
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:
@@ -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")) {
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
> : ^^^
|
||||
>{} : {}
|
||||
> : ^^
|
||||
|
||||
@@ -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() {}
|
||||
}
|
||||
|
||||
@@ -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() {}
|
||||
}
|
||||
|
||||
@@ -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() {}
|
||||
}
|
||||
|
||||
@@ -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 = {};
|
||||
@@ -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() {}
|
||||
}
|
||||
Reference in New Issue
Block a user