Skip to content

Rename strictOptionalProperties -> exactOptionalPropertyTypes and remove it from the strict family #44626

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

Merged
merged 1 commit into from
Jun 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ namespace ts {
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
const strictBindCallApply = getStrictOptionValue(compilerOptions, "strictBindCallApply");
const strictPropertyInitialization = getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
const strictOptionalProperties = getStrictOptionValue(compilerOptions, "strictOptionalProperties");
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
const useUnknownInCatchVariables = getStrictOptionValue(compilerOptions, "useUnknownInCatchVariables");
const keyofStringsOnly = !!compilerOptions.keyofStringsOnly;
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : ObjectFlags.FreshLiteral;
const exactOptionalPropertyTypes = compilerOptions.exactOptionalPropertyTypes;

const checkBinaryExpression = createCheckBinaryExpression();
const emitResolver = createResolver();
Expand Down Expand Up @@ -739,7 +739,7 @@ namespace ts {
const undefinedType = createIntrinsicType(TypeFlags.Undefined, "undefined");
const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined, "undefined", ObjectFlags.ContainsWideningType);
const optionalType = createIntrinsicType(TypeFlags.Undefined, "undefined");
const missingType = strictOptionalProperties ? createIntrinsicType(TypeFlags.Undefined, "undefined") : undefinedType;
const missingType = exactOptionalPropertyTypes ? createIntrinsicType(TypeFlags.Undefined, "undefined") : undefinedType;
const nullType = createIntrinsicType(TypeFlags.Null, "null");
const nullWideningType = strictNullChecks ? nullType : createIntrinsicType(TypeFlags.Null, "null", ObjectFlags.ContainsWideningType);
const stringType = createIntrinsicType(TypeFlags.String, "string");
Expand Down Expand Up @@ -13884,7 +13884,7 @@ namespace ts {
if (includes & TypeFlags.AnyOrUnknown) {
return includes & TypeFlags.Any ? includes & TypeFlags.IncludesWildcard ? wildcardType : anyType : unknownType;
}
if (strictOptionalProperties && includes & TypeFlags.Undefined) {
if (exactOptionalPropertyTypes && includes & TypeFlags.Undefined) {
const missingIndex = binarySearch(typeSet, missingType, getTypeId, compareValues);
if (missingIndex >= 0 && containsType(typeSet, undefinedType)) {
orderedRemoveItemAt(typeSet, missingIndex);
Expand Down Expand Up @@ -20356,15 +20356,15 @@ namespace ts {
}

function removeMissingType(type: Type, isOptional: boolean) {
return strictOptionalProperties && isOptional ? removeType(type, missingType) : type;
return exactOptionalPropertyTypes && isOptional ? removeType(type, missingType) : type;
}

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

function removeMissingOrUndefinedType(type: Type): Type {
return strictOptionalProperties ? removeType(type, missingType) : getTypeWithFacts(type, TypeFacts.NEUndefined);
return exactOptionalPropertyTypes ? removeType(type, missingType) : getTypeWithFacts(type, TypeFacts.NEUndefined);
}

/**
Expand Down Expand Up @@ -21750,7 +21750,7 @@ namespace ts {
}

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

Expand Down Expand Up @@ -26079,7 +26079,7 @@ namespace ts {
elementFlags.push(ElementFlags.Rest);
}
}
else if (strictOptionalProperties && e.kind === SyntaxKind.OmittedExpression) {
else if (exactOptionalPropertyTypes && e.kind === SyntaxKind.OmittedExpression) {
hasOmittedExpression = true;
elementTypes.push(missingType);
elementFlags.push(ElementFlags.Optional);
Expand Down
15 changes: 7 additions & 8 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,6 @@ namespace ts {
description: Diagnostics.Check_for_class_properties_that_are_declared_but_not_set_in_the_constructor,
defaultValueDescription: Diagnostics.false_unless_strict_is_set
},
{
name: "strictOptionalProperties",
type: "boolean",
affectsSemanticDiagnostics: true,
strictFlag: true,
category: Diagnostics.Type_Checking,
description: Diagnostics.Enable_strict_checking_of_optional_properties
},
{
name: "noImplicitThis",
type: "boolean",
Expand Down Expand Up @@ -700,6 +692,13 @@ namespace ts {
description: Diagnostics.Raise_an_error_when_a_function_parameter_isn_t_read,
defaultValueDescription: "false"
},
{
name: "exactOptionalPropertyTypes",
type: "boolean",
affectsSemanticDiagnostics: true,
category: Diagnostics.Type_Checking,
description: Diagnostics.Interpret_optional_property_types_as_written_rather_than_adding_undefined
},
{
name: "noImplicitReturns",
type: "boolean",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4853,7 +4853,7 @@
"category": "Message",
"code": 6242
},
"Enable strict checking of optional properties.": {
"Interpret optional property types as written, rather than adding 'undefined'.": {
"category": "Message",
"code": 6243
},
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3043,8 +3043,8 @@ 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.exactOptionalPropertyTypes && !getStrictOptionValue(options, "strictNullChecks")) {
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "exactOptionalPropertyTypes", "strictNullChecks");
}

if (options.isolatedModules) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5971,6 +5971,7 @@ namespace ts {
downlevelIteration?: boolean;
emitBOM?: boolean;
emitDecoratorMetadata?: boolean;
exactOptionalPropertyTypes?: boolean;
experimentalDecorators?: boolean;
forceConsistentCasingInFileNames?: boolean;
/*@internal*/generateCpuProfile?: string;
Expand Down Expand Up @@ -6045,7 +6046,6 @@ 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: 0 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6086,7 +6086,6 @@ namespace ts {
| "strictFunctionTypes"
| "strictBindCallApply"
| "strictPropertyInitialization"
| "strictOptionalProperties"
| "alwaysStrict"
| "useUnknownInCatchVariables"
;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,7 @@ declare namespace ts {
downlevelIteration?: boolean;
emitBOM?: boolean;
emitDecoratorMetadata?: boolean;
exactOptionalPropertyTypes?: boolean;
experimentalDecorators?: boolean;
forceConsistentCasingInFileNames?: boolean;
importHelpers?: boolean;
Expand Down Expand Up @@ -2913,7 +2914,6 @@ declare namespace ts {
strictBindCallApply?: boolean;
strictNullChecks?: boolean;
strictPropertyInitialization?: boolean;
strictOptionalProperties?: boolean;
stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,7 @@ declare namespace ts {
downlevelIteration?: boolean;
emitBOM?: boolean;
emitDecoratorMetadata?: boolean;
exactOptionalPropertyTypes?: boolean;
experimentalDecorators?: boolean;
forceConsistentCasingInFileNames?: boolean;
importHelpers?: boolean;
Expand Down Expand Up @@ -2913,7 +2914,6 @@ 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; } & T>
>Component : Component<{ x?: boolean | undefined; } & 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?: {}; }>
>props : Readonly<P> & Readonly<{ children?: {} | undefined; }>
>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?: {}; }
>props : P & { children?: {} | undefined; }
>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; }
>x : { foo?: number | undefined; }
>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; }) => 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
>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
>foo : number
>42 : 42
>foo : number
Expand Down
Loading