Skip to content

Commit 93b6354

Browse files
update implementation to use intrinsic type
1 parent fd4f646 commit 93b6354

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

src/compiler/checker.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ namespace ts {
144144
const voidType = createIntrinsicType(TypeFlags.Void, "void");
145145
const neverType = createIntrinsicType(TypeFlags.Never, "never");
146146
const silentNeverType = createIntrinsicType(TypeFlags.Never, "never");
147+
const nonPrimitiveType = createIntrinsicType(TypeFlags.NonPrimitive, "object");
147148

148149
const emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
149-
const nonPrimitiveType = createNonPrimitiveType();
150150

151151
const emptyTypeLiteralSymbol = createSymbol(SymbolFlags.TypeLiteral | SymbolFlags.Transient, "__type");
152152
emptyTypeLiteralSymbol.members = createMap<Symbol>();
@@ -1662,13 +1662,6 @@ namespace ts {
16621662
return type;
16631663
}
16641664

1665-
function createNonPrimitiveType(): ResolvedType {
1666-
const type = setStructuredTypeMembers(
1667-
createObjectType(ObjectFlags.NonPrimitive, undefined),
1668-
emptySymbols, emptyArray, emptyArray, undefined, undefined);
1669-
return type;
1670-
}
1671-
16721665
function createObjectType(objectFlags: ObjectFlags, symbol?: Symbol): ObjectType {
16731666
const type = <ObjectType>createType(TypeFlags.Object);
16741667
type.objectFlags = objectFlags;
@@ -2297,9 +2290,6 @@ namespace ts {
22972290
else if (type.flags & TypeFlags.UnionOrIntersection) {
22982291
writeUnionOrIntersectionType(<UnionOrIntersectionType>type, nextFlags);
22992292
}
2300-
else if (getObjectFlags(type) & ObjectFlags.NonPrimitive) {
2301-
writer.writeKeyword("object");
2302-
}
23032293
else if (getObjectFlags(type) & (ObjectFlags.Anonymous | ObjectFlags.Mapped)) {
23042294
writeAnonymousType(<ObjectType>type, nextFlags);
23052295
}
@@ -4748,6 +4738,7 @@ namespace ts {
47484738
t.flags & TypeFlags.NumberLike ? globalNumberType :
47494739
t.flags & TypeFlags.BooleanLike ? globalBooleanType :
47504740
t.flags & TypeFlags.ESSymbol ? getGlobalESSymbolType() :
4741+
t.flags & TypeFlags.NonPrimitive ? globalObjectType :
47514742
t;
47524743
}
47534744

@@ -7122,6 +7113,8 @@ namespace ts {
71227113
if (source.flags & TypeFlags.Enum && target.flags & TypeFlags.Enum && isEnumTypeRelatedTo(<EnumType>source, <EnumType>target, errorReporter)) return true;
71237114
if (source.flags & TypeFlags.Undefined && (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void))) return true;
71247115
if (source.flags & TypeFlags.Null && (!strictNullChecks || target.flags & TypeFlags.Null)) return true;
7116+
if (source.flags & TypeFlags.Object && target === nonPrimitiveType) return true;
7117+
if (source.flags & TypeFlags.Primitive && target === nonPrimitiveType) return false;
71257118
if (relation === assignableRelation || relation === comparableRelation) {
71267119
if (source.flags & TypeFlags.Any) return true;
71277120
if ((source.flags & TypeFlags.Number | source.flags & TypeFlags.NumberLiteral) && target.flags & TypeFlags.EnumLike) return true;
@@ -7440,7 +7433,7 @@ namespace ts {
74407433
}
74417434
}
74427435
}
7443-
else if (!(source.flags & TypeFlags.Primitive && target === nonPrimitiveType)) {
7436+
else {
74447437
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
74457438
// We have type references to same target type, see if relationship holds for all type arguments
74467439
if (result = typeArgumentsRelatedTo(<TypeReference>source, <TypeReference>target, reportErrors)) {
@@ -9190,6 +9183,9 @@ namespace ts {
91909183
}
91919184

91929185
function getTypeFacts(type: Type): TypeFacts {
9186+
if (type === nonPrimitiveType) {
9187+
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
9188+
}
91939189
const flags = type.flags;
91949190
if (flags & TypeFlags.String) {
91959191
return strictNullChecks ? TypeFacts.StringStrictFacts : TypeFacts.StringFacts;

src/compiler/types.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,7 @@ namespace ts {
27812781
ContainsObjectLiteral = 1 << 22, // Type is or contains object literal type
27822782
/* @internal */
27832783
ContainsAnyFunctionType = 1 << 23, // Type is or contains object literal type
2784+
NonPrimitive = 1 << 24, // intrinsic object type
27842785

27852786
/* @internal */
27862787
Nullable = Undefined | Null,
@@ -2790,22 +2791,22 @@ namespace ts {
27902791
DefinitelyFalsy = StringLiteral | NumberLiteral | BooleanLiteral | Void | Undefined | Null,
27912792
PossiblyFalsy = DefinitelyFalsy | String | Number | Boolean,
27922793
/* @internal */
2793-
Intrinsic = Any | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never,
2794+
Intrinsic = Any | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never | NonPrimitive,
27942795
/* @internal */
27952796
Primitive = String | Number | Boolean | Enum | ESSymbol | Void | Undefined | Null | Literal,
27962797
StringLike = String | StringLiteral | Index,
27972798
NumberLike = Number | NumberLiteral | Enum | EnumLiteral,
27982799
BooleanLike = Boolean | BooleanLiteral,
27992800
EnumLike = Enum | EnumLiteral,
28002801
UnionOrIntersection = Union | Intersection,
2801-
StructuredType = Object | Union | Intersection,
2802+
StructuredType = Object | Union | Intersection | NonPrimitive,
28022803
StructuredOrTypeParameter = StructuredType | TypeParameter | Index,
28032804
TypeVariable = TypeParameter | IndexedAccess,
28042805

28052806
// 'Narrowable' types are types where narrowing actually narrows.
28062807
// This *should* be every type other than null, undefined, void, and never
28072808
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol,
2808-
NotUnionOrUnit = Any | ESSymbol | Object,
2809+
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
28092810
/* @internal */
28102811
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
28112812
/* @internal */

tests/baselines/reference/nonPrimitiveAssignError.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(5,1): error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
2-
Property 'foo' is missing in type 'object'.
2+
Property 'foo' is missing in type 'Object'.
33
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(13,1): error TS2322: Type 'number' is not assignable to type 'object'.
44
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(14,1): error TS2322: Type 'true' is not assignable to type 'object'.
55
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(15,1): error TS2322: Type 'string' is not assignable to type 'object'.
@@ -16,7 +16,7 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(19,1): err
1616
y = a; // expect error
1717
~
1818
!!! error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
19-
!!! error TS2322: Property 'foo' is missing in type 'object'.
19+
!!! error TS2322: Property 'foo' is missing in type 'Object'.
2020
a = x;
2121
a = y;
2222

0 commit comments

Comments
 (0)