Skip to content

Commit 5afe42e

Browse files
Rename strictOptionalProperties -> exactOptionalPropertyTypes and remove from strict family (#44626)
1 parent fad9122 commit 5afe42e

File tree

88 files changed

+751
-1373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+751
-1373
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,12 @@ namespace ts {
343343
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
344344
const strictBindCallApply = getStrictOptionValue(compilerOptions, "strictBindCallApply");
345345
const strictPropertyInitialization = getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
346-
const strictOptionalProperties = getStrictOptionValue(compilerOptions, "strictOptionalProperties");
347346
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
348347
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
349348
const useUnknownInCatchVariables = getStrictOptionValue(compilerOptions, "useUnknownInCatchVariables");
350349
const keyofStringsOnly = !!compilerOptions.keyofStringsOnly;
351350
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : ObjectFlags.FreshLiteral;
351+
const exactOptionalPropertyTypes = compilerOptions.exactOptionalPropertyTypes;
352352

353353
const checkBinaryExpression = createCheckBinaryExpression();
354354
const emitResolver = createResolver();
@@ -740,7 +740,7 @@ namespace ts {
740740
const undefinedType = createIntrinsicType(TypeFlags.Undefined, "undefined");
741741
const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined, "undefined", ObjectFlags.ContainsWideningType);
742742
const optionalType = createIntrinsicType(TypeFlags.Undefined, "undefined");
743-
const missingType = strictOptionalProperties ? createIntrinsicType(TypeFlags.Undefined, "undefined") : undefinedType;
743+
const missingType = exactOptionalPropertyTypes ? createIntrinsicType(TypeFlags.Undefined, "undefined") : undefinedType;
744744
const nullType = createIntrinsicType(TypeFlags.Null, "null");
745745
const nullWideningType = strictNullChecks ? nullType : createIntrinsicType(TypeFlags.Null, "null", ObjectFlags.ContainsWideningType);
746746
const stringType = createIntrinsicType(TypeFlags.String, "string");
@@ -13886,7 +13886,7 @@ namespace ts {
1388613886
if (includes & TypeFlags.AnyOrUnknown) {
1388713887
return includes & TypeFlags.Any ? includes & TypeFlags.IncludesWildcard ? wildcardType : anyType : unknownType;
1388813888
}
13889-
if (strictOptionalProperties && includes & TypeFlags.Undefined) {
13889+
if (exactOptionalPropertyTypes && includes & TypeFlags.Undefined) {
1389013890
const missingIndex = binarySearch(typeSet, missingType, getTypeId, compareValues);
1389113891
if (missingIndex >= 0 && containsType(typeSet, undefinedType)) {
1389213892
orderedRemoveItemAt(typeSet, missingIndex);
@@ -20358,15 +20358,15 @@ namespace ts {
2035820358
}
2035920359

2036020360
function removeMissingType(type: Type, isOptional: boolean) {
20361-
return strictOptionalProperties && isOptional ? removeType(type, missingType) : type;
20361+
return exactOptionalPropertyTypes && isOptional ? removeType(type, missingType) : type;
2036220362
}
2036320363

2036420364
function containsMissingType(type: Type) {
20365-
return strictOptionalProperties && (type === missingType || type.flags & TypeFlags.Union && containsType((type as UnionType).types, missingType));
20365+
return exactOptionalPropertyTypes && (type === missingType || type.flags & TypeFlags.Union && containsType((type as UnionType).types, missingType));
2036620366
}
2036720367

2036820368
function removeMissingOrUndefinedType(type: Type): Type {
20369-
return strictOptionalProperties ? removeType(type, missingType) : getTypeWithFacts(type, TypeFacts.NEUndefined);
20369+
return exactOptionalPropertyTypes ? removeType(type, missingType) : getTypeWithFacts(type, TypeFacts.NEUndefined);
2037020370
}
2037120371

2037220372
/**
@@ -21752,7 +21752,7 @@ namespace ts {
2175221752
}
2175321753

2175421754
function isTypeOrBaseIdenticalTo(s: Type, t: Type) {
21755-
return strictOptionalProperties && t === missingType ? s === t :
21755+
return exactOptionalPropertyTypes && t === missingType ? s === t :
2175621756
(isTypeIdenticalTo(s, t) || !!(t.flags & TypeFlags.String && s.flags & TypeFlags.StringLiteral || t.flags & TypeFlags.Number && s.flags & TypeFlags.NumberLiteral));
2175721757
}
2175821758

@@ -26084,7 +26084,7 @@ namespace ts {
2608426084
elementFlags.push(ElementFlags.Rest);
2608526085
}
2608626086
}
26087-
else if (strictOptionalProperties && e.kind === SyntaxKind.OmittedExpression) {
26087+
else if (exactOptionalPropertyTypes && e.kind === SyntaxKind.OmittedExpression) {
2608826088
hasOmittedExpression = true;
2608926089
elementTypes.push(missingType);
2609026090
elementFlags.push(ElementFlags.Optional);

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,6 @@ namespace ts {
648648
description: Diagnostics.Check_for_class_properties_that_are_declared_but_not_set_in_the_constructor,
649649
defaultValueDescription: Diagnostics.false_unless_strict_is_set
650650
},
651-
{
652-
name: "strictOptionalProperties",
653-
type: "boolean",
654-
affectsSemanticDiagnostics: true,
655-
strictFlag: true,
656-
category: Diagnostics.Type_Checking,
657-
description: Diagnostics.Enable_strict_checking_of_optional_properties
658-
},
659651
{
660652
name: "noImplicitThis",
661653
type: "boolean",
@@ -700,6 +692,13 @@ namespace ts {
700692
description: Diagnostics.Raise_an_error_when_a_function_parameter_isn_t_read,
701693
defaultValueDescription: "false"
702694
},
695+
{
696+
name: "exactOptionalPropertyTypes",
697+
type: "boolean",
698+
affectsSemanticDiagnostics: true,
699+
category: Diagnostics.Type_Checking,
700+
description: Diagnostics.Interpret_optional_property_types_as_written_rather_than_adding_undefined
701+
},
703702
{
704703
name: "noImplicitReturns",
705704
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4853,7 +4853,7 @@
48534853
"category": "Message",
48544854
"code": 6242
48554855
},
4856-
"Enable strict checking of optional properties.": {
4856+
"Interpret optional property types as written, rather than adding 'undefined'.": {
48574857
"category": "Message",
48584858
"code": 6243
48594859
},

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,8 +3043,8 @@ namespace ts {
30433043
if (options.strictPropertyInitialization && !getStrictOptionValue(options, "strictNullChecks")) {
30443044
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictPropertyInitialization", "strictNullChecks");
30453045
}
3046-
if (options.strictOptionalProperties && !getStrictOptionValue(options, "strictNullChecks")) {
3047-
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "strictOptionalProperties", "strictNullChecks");
3046+
if (options.exactOptionalPropertyTypes && !getStrictOptionValue(options, "strictNullChecks")) {
3047+
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "exactOptionalPropertyTypes", "strictNullChecks");
30483048
}
30493049

30503050
if (options.isolatedModules) {

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5972,6 +5972,7 @@ namespace ts {
59725972
downlevelIteration?: boolean;
59735973
emitBOM?: boolean;
59745974
emitDecoratorMetadata?: boolean;
5975+
exactOptionalPropertyTypes?: boolean;
59755976
experimentalDecorators?: boolean;
59765977
forceConsistentCasingInFileNames?: boolean;
59775978
/*@internal*/generateCpuProfile?: string;
@@ -6046,7 +6047,6 @@ namespace ts {
60466047
strictBindCallApply?: boolean; // Always combine with strict property
60476048
strictNullChecks?: boolean; // Always combine with strict property
60486049
strictPropertyInitialization?: boolean; // Always combine with strict property
6049-
strictOptionalProperties?: boolean; // Always combine with strict property
60506050
stripInternal?: boolean;
60516051
suppressExcessPropertyErrors?: boolean;
60526052
suppressImplicitAnyIndexErrors?: boolean;

src/compiler/utilities.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6098,7 +6098,6 @@ namespace ts {
60986098
| "strictFunctionTypes"
60996099
| "strictBindCallApply"
61006100
| "strictPropertyInitialization"
6101-
| "strictOptionalProperties"
61026101
| "alwaysStrict"
61036102
| "useUnknownInCatchVariables"
61046103
;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,7 @@ declare namespace ts {
28532853
downlevelIteration?: boolean;
28542854
emitBOM?: boolean;
28552855
emitDecoratorMetadata?: boolean;
2856+
exactOptionalPropertyTypes?: boolean;
28562857
experimentalDecorators?: boolean;
28572858
forceConsistentCasingInFileNames?: boolean;
28582859
importHelpers?: boolean;
@@ -2913,7 +2914,6 @@ declare namespace ts {
29132914
strictBindCallApply?: boolean;
29142915
strictNullChecks?: boolean;
29152916
strictPropertyInitialization?: boolean;
2916-
strictOptionalProperties?: boolean;
29172917
stripInternal?: boolean;
29182918
suppressExcessPropertyErrors?: boolean;
29192919
suppressImplicitAnyIndexErrors?: boolean;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,7 @@ declare namespace ts {
28532853
downlevelIteration?: boolean;
28542854
emitBOM?: boolean;
28552855
emitDecoratorMetadata?: boolean;
2856+
exactOptionalPropertyTypes?: boolean;
28562857
experimentalDecorators?: boolean;
28572858
forceConsistentCasingInFileNames?: boolean;
28582859
importHelpers?: boolean;
@@ -2913,7 +2914,6 @@ declare namespace ts {
29132914
strictBindCallApply?: boolean;
29142915
strictNullChecks?: boolean;
29152916
strictPropertyInitialization?: boolean;
2916-
strictOptionalProperties?: boolean;
29172917
stripInternal?: boolean;
29182918
suppressExcessPropertyErrors?: boolean;
29192919
suppressImplicitAnyIndexErrors?: boolean;

tests/baselines/reference/checkJsxIntersectionElementPropsType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare class Component<P> {
1616

1717
class C<T> extends Component<{ x?: boolean; } & T> {}
1818
>C : C<T>
19-
>Component : Component<{ x?: boolean; } & T>
19+
>Component : Component<{ x?: boolean | undefined; } & T>
2020
>x : boolean | undefined
2121

2222
const y = new C({foobar: "example"});

tests/baselines/reference/circularlyConstrainedMappedTypeContainingConditionalNoInfiniteInstantiationDepth.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare class Component<P> {
1010
>context : any
1111

1212
readonly props: Readonly<P> & Readonly<{ children?: {} }>;
13-
>props : Readonly<P> & Readonly<{ children?: {}; }>
13+
>props : Readonly<P> & Readonly<{ children?: {} | undefined; }>
1414
>children : {} | undefined
1515
}
1616
interface ComponentClass<P = {}> {
@@ -29,7 +29,7 @@ interface ComponentClass<P = {}> {
2929
}
3030
interface FunctionComponent<P = {}> {
3131
(props: P & { children?: {} }, context?: any): {} | null;
32-
>props : P & { children?: {}; }
32+
>props : P & { children?: {} | undefined; }
3333
>children : {} | undefined
3434
>context : any
3535
>null : null

tests/baselines/reference/contextuallyTypedParametersWithInitializers.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare function id3<T extends (x: { foo: any }) => any>(input: T): T;
1616

1717
declare function id4<T extends (x: { foo?: number }) => any>(input: T): T;
1818
>id4 : <T extends (x: { foo?: number;}) => any>(input: T) => T
19-
>x : { foo?: number; }
19+
>x : { foo?: number | undefined; }
2020
>foo : number | undefined
2121
>input : T
2222

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

6262
const f14 = id4(function ({ foo = 42 }) { return foo });
63-
>f14 : ({ foo }: { foo?: number; }) => number
64-
>id4(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number; }) => number
65-
>id4 : <T extends (x: { foo?: number; }) => any>(input: T) => T
66-
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number; }) => number
63+
>f14 : ({ foo }: { foo?: number | undefined; }) => number
64+
>id4(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number | undefined; }) => number
65+
>id4 : <T extends (x: { foo?: number | undefined; }) => any>(input: T) => T
66+
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number | undefined; }) => number
6767
>foo : number
6868
>42 : 42
6969
>foo : number

0 commit comments

Comments
 (0)