Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict optional properties #43947

Merged
merged 18 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
184 changes: 101 additions & 83 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@ namespace ts {
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_checking_of_property_initialization_in_classes
},
{
name: "strictOptionalProperties",
type: "boolean",
affectsSemanticDiagnostics: true,
strictFlag: true,
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_checking_of_optional_properties
},
{
name: "noImplicitThis",
type: "boolean",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4873,6 +4873,10 @@
"category": "Message",
"code": 6242
},
"Enable strict checking of optional properties.": {
"category": "Message",
"code": 6243
},

"Projects to reference": {
"category": "Message",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,9 @@ namespace ts {
if (options.strictPropertyInitialization && !getStrictOptionValue(options, "strictNullChecks")) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictPropertyInitialization", "strictNullChecks");
}
if (options.strictOptionalProperties && !getStrictOptionValue(options, "strictNullChecks")) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictOptionalProperties", "strictNullChecks");
}

if (options.isolatedModules) {
if (options.out) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6041,6 +6041,7 @@ namespace ts {
strictBindCallApply?: boolean; // Always combine with strict property
strictNullChecks?: boolean; // Always combine with strict property
strictPropertyInitialization?: boolean; // Always combine with strict property
strictOptionalProperties?: boolean; // Always combine with strict property
stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6109,6 +6109,7 @@ namespace ts {
| "strictFunctionTypes"
| "strictBindCallApply"
| "strictPropertyInitialization"
| "strictOptionalProperties"
| "alwaysStrict"
;

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,7 @@ declare namespace ts {
strictBindCallApply?: boolean;
strictNullChecks?: boolean;
strictPropertyInitialization?: boolean;
strictOptionalProperties?: boolean;
stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,7 @@ declare namespace ts {
strictBindCallApply?: boolean;
strictNullChecks?: boolean;
strictPropertyInitialization?: boolean;
strictOptionalProperties?: boolean;
stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare class Component<P> {

class C<T> extends Component<{ x?: boolean; } & T> {}
>C : C<T>
>Component : Component<{ x?: boolean | undefined; } & T>
>Component : Component<{ x?: boolean; } & T>
>x : boolean | undefined

const y = new C({foobar: "example"});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare class Component<P> {
>context : any

readonly props: Readonly<P> & Readonly<{ children?: {} }>;
>props : Readonly<P> & Readonly<{ children?: {} | undefined; }>
>props : Readonly<P> & Readonly<{ children?: {}; }>
>children : {} | undefined
}
interface ComponentClass<P = {}> {
Expand All @@ -29,7 +29,7 @@ interface ComponentClass<P = {}> {
}
interface FunctionComponent<P = {}> {
(props: P & { children?: {} }, context?: any): {} | null;
>props : P & { children?: {} | undefined; }
>props : P & { children?: {}; }
>children : {} | undefined
>context : any
>null : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare function id3<T extends (x: { foo: any }) => any>(input: T): T;

declare function id4<T extends (x: { foo?: number }) => any>(input: T): T;
>id4 : <T extends (x: { foo?: number;}) => any>(input: T) => T
>x : { foo?: number | undefined; }
>x : { foo?: number; }
>foo : number | undefined
>input : T

Expand Down Expand Up @@ -60,10 +60,10 @@ const f13 = id3(function ({ foo = 42 }) { return foo });
>foo : any

const f14 = id4(function ({ foo = 42 }) { return foo });
>f14 : ({ foo }: { foo?: number | undefined; }) => number
>id4(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number | undefined; }) => number
>id4 : <T extends (x: { foo?: number | undefined; }) => any>(input: T) => T
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number | undefined; }) => number
>f14 : ({ foo }: { foo?: number; }) => number
>id4(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number; }) => number
>id4 : <T extends (x: { foo?: number; }) => any>(input: T) => T
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number; }) => number
>foo : number
>42 : 42
>foo : number
Expand Down