Skip to content

Ensure singleton types always compare identical #44565

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 3 commits into from
Jun 13, 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
54 changes: 19 additions & 35 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,7 @@ namespace ts {
falseType.freshType = falseType;
regularFalseType.regularType = regularFalseType;
regularFalseType.freshType = falseType;
const booleanType = createBooleanType([regularFalseType, regularTrueType]);
// Also mark all combinations of fresh/regular booleans as "Boolean" so they print as `boolean` instead of `true | false`
// (The union is cached, so simply doing the marking here is sufficient)
createBooleanType([regularFalseType, trueType]);
createBooleanType([falseType, regularTrueType]);
createBooleanType([falseType, trueType]);
const booleanType = getUnionType([regularFalseType, regularTrueType]);
const esSymbolType = createIntrinsicType(TypeFlags.ESSymbol, "symbol");
const voidType = createIntrinsicType(TypeFlags.Void, "void");
const neverType = createIntrinsicType(TypeFlags.Never, "never");
Expand Down Expand Up @@ -3836,13 +3831,6 @@ namespace ts {
return type;
}

function createBooleanType(trueFalseTypes: readonly Type[]): IntrinsicType & UnionType {
const type = getUnionType(trueFalseTypes) as IntrinsicType & UnionType;
type.flags |= TypeFlags.Boolean;
type.intrinsicName = "boolean";
return type;
}

function createObjectType(objectFlags: ObjectFlags, symbol?: Symbol): ObjectType {
const type = createType(TypeFlags.Object) as ObjectType;
type.objectFlags = objectFlags;
Expand Down Expand Up @@ -4583,7 +4571,7 @@ namespace ts {
context.approximateLength += 6;
return factory.createKeywordTypeNode(SyntaxKind.BigIntKeyword);
}
if (type.flags & TypeFlags.Boolean) {
if (type.flags & TypeFlags.Boolean && !type.aliasSymbol) {
context.approximateLength += 7;
return factory.createKeywordTypeNode(SyntaxKind.BooleanKeyword);
}
Expand Down Expand Up @@ -13974,16 +13962,6 @@ namespace ts {
return a.kind === b.kind && a.parameterIndex === b.parameterIndex;
}

function createUnionType(types: Type[], aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[], origin?: Type) {
const result = createType(TypeFlags.Union) as UnionType;
result.objectFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
result.types = types;
result.origin = origin;
result.aliasSymbol = aliasSymbol;
result.aliasTypeArguments = aliasTypeArguments;
return result;
}

// This function assumes the constituent type list is sorted and deduplicated.
function getUnionTypeFromSortedList(types: Type[], objectFlags: ObjectFlags, aliasSymbol?: Symbol, aliasTypeArguments?: readonly Type[], origin?: Type): Type {
if (types.length === 0) {
Expand All @@ -13999,8 +13977,16 @@ namespace ts {
const id = typeKey + getAliasId(aliasSymbol, aliasTypeArguments);
let type = unionTypes.get(id);
if (!type) {
type = createUnionType(types, aliasSymbol, aliasTypeArguments, origin);
type.objectFlags |= objectFlags;
type = createType(TypeFlags.Union) as UnionType;
type.objectFlags = objectFlags | getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
type.types = types;
type.origin = origin;
type.aliasSymbol = aliasSymbol;
type.aliasTypeArguments = aliasTypeArguments;
if (types.length === 2 && types[0].flags & TypeFlags.BooleanLiteral && types[1].flags & TypeFlags.BooleanLiteral) {
type.flags |= TypeFlags.Boolean;
(type as UnionType & IntrinsicType).intrinsicName = "boolean";
}
unionTypes.set(id, type);
}
return type;
Expand Down Expand Up @@ -17328,8 +17314,8 @@ namespace ts {
}
}
else {
if (!(source.flags & TypeFlags.UnionOrIntersection) && !(target.flags & TypeFlags.UnionOrIntersection) &&
source.flags !== target.flags && !(source.flags & TypeFlags.Substructure)) return false;
if (source.flags !== target.flags) return false;
if (source.flags & TypeFlags.Singleton) return true;
}
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) {
const related = relation.get(getRelationKey(source, target, IntersectionState.None, relation));
Expand Down Expand Up @@ -17931,12 +17917,10 @@ namespace ts {
}

function isIdenticalTo(source: Type, target: Type): Ternary {
const flags = source.flags & target.flags;
if (!(flags & TypeFlags.Substructure)) {
return Ternary.False;
}
if (source.flags !== target.flags) return Ternary.False;
if (source.flags & TypeFlags.Singleton) return Ternary.True;
traceUnionsOrIntersectionsTooLarge(source, target);
if (flags & TypeFlags.UnionOrIntersection) {
if (source.flags & TypeFlags.UnionOrIntersection) {
let result = eachTypeRelatedToSomeType(source as UnionOrIntersectionType, target as UnionOrIntersectionType);
if (result) {
result &= eachTypeRelatedToSomeType(target as UnionOrIntersectionType, source as UnionOrIntersectionType);
Expand Down Expand Up @@ -21131,7 +21115,6 @@ namespace ts {
inferFromTypes(originalSource, originalTarget);

function inferFromTypes(source: Type, target: Type): void {

if (!couldContainTypeVariables(target)) {
return;
}
Expand Down Expand Up @@ -21749,7 +21732,8 @@ namespace ts {
}

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

function isTypeCloselyMatchedBy(s: Type, t: Type) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5086,7 +5086,7 @@ namespace ts {
/* @internal */
Simplifiable = IndexedAccess | Conditional,
/* @internal */
Substructure = Object | Union | Intersection | Index | IndexedAccess | Conditional | Substitution | TemplateLiteral | StringMapping,
Singleton = Any | Unknown | String | Number | Boolean | BigInt | ESSymbol | Void | Undefined | Null | Never | NonPrimitive,
// 'Narrowable' types are types where narrowing actually narrows.
// This *should* be every type other than null, undefined, void, and never
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
Expand Down
60 changes: 60 additions & 0 deletions tests/baselines/reference/strictOptionalProperties1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,64 @@ tests/cases/compiler/strictOptionalProperties1.ts(119,5): error TS2411: Property
~~~
!!! error TS2411: Property 'bar' of type 'string | undefined' is not assignable to string index type 'string'.
}

// Strict optional properties and inference

declare let ox1: { p: string };
declare let ox2: { p: string | undefined };
declare let ox3: { p?: string };
declare let ox4: { p?: string | undefined };

declare let tx1: [string];
declare let tx2: [string | undefined];
declare let tx3: [string?];
declare let tx4: [(string | undefined)?];

declare function f11<T>(x: { p?: T }): T;

f11(ox1); // string
f11(ox2); // string | undefined
f11(ox3); // string
f11(ox4); // string | undefined

declare function f12<T>(x: [T?]): T;

f12(tx1); // string
f12(tx2); // string | undefined
f12(tx3); // string
f12(tx4); // string | undefined

declare function f13<T>(x: Partial<T>): T;

f13(ox1); // { p: string }
f13(ox2); // { p: string | undefined }
f13(ox3); // { p: string }
f13(ox4); // { p: string | undefined }

f13(tx1); // [string]
f13(tx2); // [string | undefined]
f13(tx3); // [string]
f13(tx4); // [string | undefined]

// Repro from #44388

type Undefinable<T> = T | undefined;

function expectNotUndefined<T>(value: Undefinable<T>): T {
if (value === undefined) {
throw new TypeError('value is undefined');
}
return value;
}

interface Bar {
bar?: number;
}

function aa(input: Bar): void {
const notUndefinedVal = expectNotUndefined(input.bar);
bb(notUndefinedVal);
}

declare function bb(input: number): void;

114 changes: 114 additions & 0 deletions tests/baselines/reference/strictOptionalProperties1.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,66 @@ interface Test {
foo?: string; // Should be ok
bar?: string | undefined; // Error
}

// Strict optional properties and inference

declare let ox1: { p: string };
declare let ox2: { p: string | undefined };
declare let ox3: { p?: string };
declare let ox4: { p?: string | undefined };

declare let tx1: [string];
declare let tx2: [string | undefined];
declare let tx3: [string?];
declare let tx4: [(string | undefined)?];

declare function f11<T>(x: { p?: T }): T;

f11(ox1); // string
f11(ox2); // string | undefined
f11(ox3); // string
f11(ox4); // string | undefined

declare function f12<T>(x: [T?]): T;

f12(tx1); // string
f12(tx2); // string | undefined
f12(tx3); // string
f12(tx4); // string | undefined

declare function f13<T>(x: Partial<T>): T;

f13(ox1); // { p: string }
f13(ox2); // { p: string | undefined }
f13(ox3); // { p: string }
f13(ox4); // { p: string | undefined }

f13(tx1); // [string]
f13(tx2); // [string | undefined]
f13(tx3); // [string]
f13(tx4); // [string | undefined]

// Repro from #44388

type Undefinable<T> = T | undefined;

function expectNotUndefined<T>(value: Undefinable<T>): T {
if (value === undefined) {
throw new TypeError('value is undefined');
}
return value;
}

interface Bar {
bar?: number;
}

function aa(input: Bar): void {
const notUndefinedVal = expectNotUndefined(input.bar);
bb(notUndefinedVal);
}

declare function bb(input: number): void;


//// [strictOptionalProperties1.js]
Expand Down Expand Up @@ -223,6 +283,32 @@ var t4 = [1, undefined, undefined];
// Example from #13195
var x = { foo: undefined };
var y = __assign({ foo: 123 }, x);
f11(ox1); // string
f11(ox2); // string | undefined
f11(ox3); // string
f11(ox4); // string | undefined
f12(tx1); // string
f12(tx2); // string | undefined
f12(tx3); // string
f12(tx4); // string | undefined
f13(ox1); // { p: string }
f13(ox2); // { p: string | undefined }
f13(ox3); // { p: string }
f13(ox4); // { p: string | undefined }
f13(tx1); // [string]
f13(tx2); // [string | undefined]
f13(tx3); // [string]
f13(tx4); // [string | undefined]
function expectNotUndefined(value) {
if (value === undefined) {
throw new TypeError('value is undefined');
}
return value;
}
function aa(input) {
var notUndefinedVal = expectNotUndefined(input.bar);
bb(notUndefinedVal);
}


//// [strictOptionalProperties1.d.ts]
Expand Down Expand Up @@ -268,3 +354,31 @@ interface Test {
foo?: string;
bar?: string | undefined;
}
declare let ox1: {
p: string;
};
declare let ox2: {
p: string | undefined;
};
declare let ox3: {
p?: string;
};
declare let ox4: {
p?: string | undefined;
};
declare let tx1: [string];
declare let tx2: [string | undefined];
declare let tx3: [string?];
declare let tx4: [(string | undefined)?];
declare function f11<T>(x: {
p?: T;
}): T;
declare function f12<T>(x: [T?]): T;
declare function f13<T>(x: Partial<T>): T;
declare type Undefinable<T> = T | undefined;
declare function expectNotUndefined<T>(value: Undefinable<T>): T;
interface Bar {
bar?: number;
}
declare function aa(input: Bar): void;
declare function bb(input: number): void;
Loading