Skip to content

Commit

Permalink
Cache isWeakType computation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Feb 8, 2022
1 parent 530c876 commit 25a71c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20258,15 +20258,22 @@ namespace ts {
* and no required properties, call/construct signatures or index signatures
*/
function isWeakType(type: Type): boolean {
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
return resolved.callSignatures.length === 0 && resolved.constructSignatures.length === 0 && resolved.indexInfos.length === 0 &&
resolved.properties.length > 0 && every(resolved.properties, p => !!(p.flags & SymbolFlags.Optional));
if (!(type.flags & (TypeFlags.Object | TypeFlags.Intersection))) {
return false;
}
if (type.flags & TypeFlags.Intersection) {
return every((type as IntersectionType).types, isWeakType);
if (!((type as ObjectType | IntersectionType).objectFlags & ObjectFlags.IsWeakTypeComputed)) {
let isWeak;
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
isWeak = resolved.callSignatures.length === 0 && resolved.constructSignatures.length === 0 && resolved.indexInfos.length === 0 &&
resolved.properties.length > 0 && every(resolved.properties, p => !!(p.flags & SymbolFlags.Optional));
}
else {
isWeak = every((type as IntersectionType).types, isWeakType);
}
(type as ObjectType | IntersectionType).objectFlags |= ObjectFlags.IsWeakTypeComputed | (isWeak ? ObjectFlags.IsWeakType : 0);
}
return false;
return !!((type as ObjectType | IntersectionType).objectFlags & ObjectFlags.IsWeakType);
}

function hasCommonProperties(source: Type, target: Type, isComparingJsxAttributes: boolean) {
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5360,6 +5360,12 @@ namespace ts {
IsNeverIntersectionComputed = 1 << 25, // IsNeverLike flag has been computed
/* @internal */
IsNeverIntersection = 1 << 26, // Intersection reduces to never

// Flags that require TypeFlags.Object or TypeFlags.Intersection
/* @internal */
IsWeakTypeComputed = 1 << 27,
/* @internal */
IsWeakType = 1 << 28,
}

/* @internal */
Expand Down

0 comments on commit 25a71c4

Please sign in to comment.