Remove 'T extends U' type constructor

This commit is contained in:
Anders Hejlsberg 2018-01-15 08:11:05 -08:00
parent 53b1572ed6
commit 5094f7677c
10 changed files with 8 additions and 146 deletions

View File

@ -3428,7 +3428,6 @@ namespace ts {
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.ThisType:
case SyntaxKind.TypeOperator:
case SyntaxKind.BinaryType:
case SyntaxKind.IndexedAccessType:
case SyntaxKind.MappedType:
case SyntaxKind.LiteralType:

View File

@ -281,7 +281,6 @@ namespace ts {
const literalTypes = createMap<LiteralType>();
const indexedAccessTypes = createMap<IndexedAccessType>();
const conditionalTypes = createMap<ConditionalType>();
const extendsTypes = createMap<ExtendsType>();
const evolvingArrayTypes: EvolvingArrayType[] = [];
const undefinedProperties = createMap<Symbol>() as UnderscoreEscapedMap<Symbol>;
@ -2649,11 +2648,6 @@ namespace ts {
const falseTypeNode = typeToTypeNodeHelper((<ConditionalType>type).falseType, context);
return createConditionalTypeNode(checkTypeNode, extendsTypeNode, trueTypeNode, falseTypeNode);
}
if (type.flags & TypeFlags.Extends) {
const leftTypeNode = typeToTypeNodeHelper((<ExtendsType>type).checkType, context);
const rightTypeNode = typeToTypeNodeHelper((<ExtendsType>type).extendsType, context);
return createBinaryTypeNode(leftTypeNode, SyntaxKind.ExtendsKeyword, rightTypeNode);
}
Debug.fail("Should be unreachable.");
@ -3437,13 +3431,6 @@ namespace ts {
writeSpace(writer);
writeType((<ConditionalType>type).falseType, TypeFormatFlags.InElementType);
}
else if (type.flags & TypeFlags.Extends) {
writeType((<ExtendsType>type).checkType, TypeFormatFlags.InElementType);
writeSpace(writer);
writer.writeKeyword("extends");
writeSpace(writer);
writeType((<ExtendsType>type).extendsType, TypeFormatFlags.InElementType);
}
else {
// Should never get here
// { ... }
@ -6425,9 +6412,6 @@ namespace ts {
else if (type.flags & TypeFlags.Index) {
return stringType;
}
else if (type.flags & TypeFlags.Extends) {
return booleanType;
}
return undefined;
}
@ -6496,9 +6480,6 @@ namespace ts {
if (t.flags & TypeFlags.Conditional) {
return getBaseConstraint(getConstraintOfConditionalType(<ConditionalType>t));
}
if (t.flags & TypeFlags.Extends) {
return booleanType;
}
if (isGenericMappedType(t)) {
return emptyObjectType;
}
@ -8435,47 +8416,6 @@ namespace ts {
return links.resolvedType;
}
function createExtendsType(checkType: Type, extendsType: Type) {
const type = <ExtendsType>createType(TypeFlags.Extends);
type.checkType = checkType;
type.extendsType = extendsType;
return type;
}
function getExtendsType(checkType: Type, extendsType: Type): Type {
// sys.write(`getExtendsType(${typeToString(checkType)}, ${typeToString(extendsType)})\n`);
if (checkType.flags & TypeFlags.Union) {
return getUnionType(map((<UnionType>checkType).types, t => getExtendsType(t, extendsType)));
}
if (checkType.flags & TypeFlags.Any) {
return booleanType;
}
// Return trueType if type is definitely assignable
if (isTypeAssignableTo(checkType, extendsType)) {
return trueType;
}
// Return falseType is type is definitely not assignable
if (!isTypeAssignableTo(instantiateType(checkType, anyMapper), instantiateType(extendsType, constraintMapper))) {
// Type is definitely not assignable
return falseType;
}
// Type is possibly assignable, defer the check
const id = checkType.id + "," + extendsType.id;
let type = extendsTypes.get(id);
if (!type) {
extendsTypes.set(id, type = createExtendsType(checkType, extendsType));
}
return type;
}
function getTypeFromBinaryTypeNode(node: BinaryTypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
links.resolvedType = getExtendsType(getTypeFromTypeNode(node.left), getTypeFromTypeNode(node.right));
}
return links.resolvedType;
}
function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node: TypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
@ -8766,8 +8706,6 @@ namespace ts {
return getTypeFromMappedTypeNode(<MappedTypeNode>node);
case SyntaxKind.ConditionalType:
return getTypeFromConditionalTypeNode(<ConditionalTypeNode>node);
case SyntaxKind.BinaryType:
return getTypeFromBinaryTypeNode(<BinaryTypeNode>node);
// This function assumes that an identifier or qualified name is a type expression
// Callers should first ensure this by calling isTypeNode
case SyntaxKind.Identifier:
@ -9096,9 +9034,6 @@ namespace ts {
if (type.flags & TypeFlags.Conditional) {
return getConditionalTypeInstantiation(<ConditionalType>type, mapper);
}
if (type.flags & TypeFlags.Extends) {
return getExtendsType(instantiateType((<ExtendsType>type).checkType, mapper), instantiateType((<ExtendsType>type).extendsType, mapper));
}
}
return type;
}
@ -11652,10 +11587,6 @@ namespace ts {
inferFromTypes((<ConditionalType>source).trueType, (<ConditionalType>target).trueType);
inferFromTypes((<ConditionalType>source).falseType, (<ConditionalType>target).falseType);
}
else if (source.flags & TypeFlags.Extends && target.flags & TypeFlags.Extends) {
inferFromTypes((<ExtendsType>source).checkType, (<ExtendsType>target).checkType);
inferFromTypes((<ExtendsType>source).extendsType, (<ExtendsType>target).extendsType);
}
else if (target.flags & TypeFlags.UnionOrIntersection) {
const targetTypes = (<UnionOrIntersectionType>target).types;
let typeVariableCount = 0;
@ -24015,9 +23946,6 @@ namespace ts {
return checkTypeOperator(<TypeOperatorNode>node);
case SyntaxKind.ConditionalType:
return checkConditionalType(<ConditionalTypeNode>node);
case SyntaxKind.BinaryType:
forEachChild(node, checkSourceElement);
return;
case SyntaxKind.JSDocAugmentsTag:
return checkJSDocAugmentsTag(node as JSDocAugmentsTag);
case SyntaxKind.JSDocTypedefTag:

View File

@ -456,8 +456,6 @@ namespace ts {
return emitParenType(<ParenthesizedTypeNode>type);
case SyntaxKind.TypeOperator:
return emitTypeOperator(<TypeOperatorNode>type);
case SyntaxKind.BinaryType:
return emitBinaryType(<BinaryTypeNode>type);
case SyntaxKind.IndexedAccessType:
return emitIndexedAccessType(<IndexedAccessTypeNode>type);
case SyntaxKind.MappedType:
@ -571,12 +569,6 @@ namespace ts {
emitType(type.type);
}
function emitBinaryType(node: BinaryTypeNode) {
emitType(node.left);
write(" extends ");
emitType(node.right);
}
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
emitType(node.objectType);
write("[");

View File

@ -572,8 +572,6 @@ namespace ts {
return emitThisType();
case SyntaxKind.TypeOperator:
return emitTypeOperator(<TypeOperatorNode>node);
case SyntaxKind.BinaryType:
return emitBinaryType(<BinaryTypeNode>node);
case SyntaxKind.IndexedAccessType:
return emitIndexedAccessType(<IndexedAccessTypeNode>node);
case SyntaxKind.MappedType:
@ -1159,12 +1157,6 @@ namespace ts {
emit(node.type);
}
function emitBinaryType(node: BinaryTypeNode) {
emit(node.left);
write(" extends ");
emit(node.right);
}
function emitIndexedAccessType(node: IndexedAccessTypeNode) {
emit(node.objectType);
write("[");

View File

@ -767,22 +767,6 @@ namespace ts {
return node.type !== type ? updateNode(createTypeOperatorNode(node.operator, type), node) : node;
}
export function createBinaryTypeNode(left: TypeNode, operator: SyntaxKind.ExtendsKeyword, right: TypeNode) {
const node = createSynthesizedNode(SyntaxKind.BinaryType) as BinaryTypeNode;
node.left = parenthesizeBinaryTypeMember(left);
node.operator = operator;
node.right = parenthesizeBinaryTypeMember(right);
return node;
}
export function updateBinaryTypeNode(node: BinaryTypeNode, left: TypeNode, operator: SyntaxKind.ExtendsKeyword, right: TypeNode) {
return node.left !== left
|| node.operator !== operator
|| node.right !== right
? updateNode(createBinaryTypeNode(left, operator, right), node)
: node;
}
export function createIndexedAccessTypeNode(objectType: TypeNode, indexType: TypeNode) {
const node = createSynthesizedNode(SyntaxKind.IndexedAccessType) as IndexedAccessTypeNode;
node.objectType = parenthesizeElementTypeMember(objectType);
@ -4119,10 +4103,6 @@ namespace ts {
return member.kind === SyntaxKind.ConditionalType ? createParenthesizedType(member) : member;
}
export function parenthesizeBinaryTypeMember(member: TypeNode) {
return member.kind === SyntaxKind.BinaryType ? createParenthesizedType(member) : parenthesizeConditionalTypeMember(member);
}
export function parenthesizeElementTypeMember(member: TypeNode) {
switch (member.kind) {
case SyntaxKind.UnionType:
@ -4131,7 +4111,7 @@ namespace ts {
case SyntaxKind.ConstructorType:
return createParenthesizedType(member);
}
return parenthesizeBinaryTypeMember(member);
return parenthesizeConditionalTypeMember(member);
}
export function parenthesizeArrayTypeMember(member: TypeNode) {

View File

@ -183,9 +183,6 @@ namespace ts {
case SyntaxKind.ParenthesizedType:
case SyntaxKind.TypeOperator:
return visitNode(cbNode, (<ParenthesizedTypeNode | TypeOperatorNode>node).type);
case SyntaxKind.BinaryType:
return visitNode(cbNode, (<BinaryTypeNode>node).left) ||
visitNode(cbNode, (<BinaryTypeNode>node).right);
case SyntaxKind.IndexedAccessType:
return visitNode(cbNode, (<IndexedAccessTypeNode>node).objectType) ||
visitNode(cbNode, (<IndexedAccessTypeNode>node).indexType);

View File

@ -389,7 +389,6 @@ namespace ts {
case SyntaxKind.ParenthesizedType:
case SyntaxKind.ThisType:
case SyntaxKind.TypeOperator:
case SyntaxKind.BinaryType:
case SyntaxKind.IndexedAccessType:
case SyntaxKind.MappedType:
case SyntaxKind.LiteralType:
@ -1887,7 +1886,6 @@ namespace ts {
case SyntaxKind.TypeQuery:
case SyntaxKind.TypeOperator:
case SyntaxKind.BinaryType:
case SyntaxKind.IndexedAccessType:
case SyntaxKind.MappedType:
case SyntaxKind.TypeLiteral:

View File

@ -252,7 +252,6 @@ namespace ts {
ParenthesizedType,
ThisType,
TypeOperator,
BinaryType,
IndexedAccessType,
MappedType,
LiteralType,
@ -1116,13 +1115,6 @@ namespace ts {
type: TypeNode;
}
export interface BinaryTypeNode extends TypeNode {
kind: SyntaxKind.BinaryType;
left: TypeNode;
operator: SyntaxKind.ExtendsKeyword;
right: TypeNode;
}
/* @internal */
export interface UniqueTypeOperatorNode extends TypeOperatorNode {
operator: SyntaxKind.UniqueKeyword;
@ -3409,16 +3401,15 @@ namespace ts {
Index = 1 << 19, // keyof T
IndexedAccess = 1 << 20, // T[K]
Conditional = 1 << 21, // T extends U ? X : Y
Extends = 1 << 22, // T extends U
/* @internal */
FreshLiteral = 1 << 23, // Fresh literal or unique type
FreshLiteral = 1 << 22, // Fresh literal or unique type
/* @internal */
ContainsWideningType = 1 << 24, // Type is or contains undefined or null widening type
ContainsWideningType = 1 << 23, // Type is or contains undefined or null widening type
/* @internal */
ContainsObjectLiteral = 1 << 25, // Type is or contains object literal type
ContainsObjectLiteral = 1 << 24, // Type is or contains object literal type
/* @internal */
ContainsAnyFunctionType = 1 << 26, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 27, // intrinsic object type
ContainsAnyFunctionType = 1 << 25, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 26, // intrinsic object type
/* @internal */
GenericMappedType = 1 << 29, // Flag used by maybeTypeOfKind
@ -3438,14 +3429,14 @@ namespace ts {
Primitive = String | Number | Boolean | Enum | EnumLiteral | ESSymbol | Void | Undefined | Null | Literal | UniqueESSymbol,
StringLike = String | StringLiteral | Index,
NumberLike = Number | NumberLiteral | Enum,
BooleanLike = Boolean | BooleanLiteral | Extends,
BooleanLike = Boolean | BooleanLiteral,
EnumLike = Enum | EnumLiteral,
ESSymbolLike = ESSymbol | UniqueESSymbol,
UnionOrIntersection = Union | Intersection,
StructuredType = Object | Union | Intersection,
TypeVariable = TypeParameter | IndexedAccess,
InstantiableNonPrimitive = TypeVariable | Conditional,
InstantiablePrimitive = Index | Extends,
InstantiablePrimitive = Index,
Instantiable = InstantiableNonPrimitive | InstantiablePrimitive,
StructuredOrInstantiable = StructuredType | Instantiable,
@ -3709,11 +3700,6 @@ namespace ts {
falseType: Type;
}
export interface ExtendsType extends InstantiableType {
checkType: Type;
extendsType: Type;
}
export const enum SignatureKind {
Call,
Construct,

View File

@ -4549,10 +4549,6 @@ namespace ts {
return node.kind === SyntaxKind.TypeOperator;
}
export function isBinaryTypeNode(node: Node): node is BinaryTypeNode {
return node.kind === SyntaxKind.BinaryType;
}
export function isIndexedAccessTypeNode(node: Node): node is IndexedAccessTypeNode {
return node.kind === SyntaxKind.IndexedAccessType;
}

View File

@ -400,12 +400,6 @@ namespace ts {
return updateTypeOperatorNode(<TypeOperatorNode>node,
visitNode((<TypeOperatorNode>node).type, visitor, isTypeNode));
case SyntaxKind.BinaryType:
return updateBinaryTypeNode(<BinaryTypeNode>node,
visitNode((<BinaryTypeNode>node).left, visitor, isTypeNode),
(<BinaryTypeNode>node).operator,
visitNode((<BinaryTypeNode>node).right, visitor, isTypeNode));
case SyntaxKind.IndexedAccessType:
return updateIndexedAccessTypeNode((<IndexedAccessTypeNode>node),
visitNode((<IndexedAccessTypeNode>node).objectType, visitor, isTypeNode),