Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
57ca768
Initial implementation of conditional type operator
ahejlsberg Dec 5, 2017
063eed1
Add type relationships and distribute over union types
ahejlsberg Dec 8, 2017
ec2bdfd
Add 'T extends U' type operator
ahejlsberg Dec 12, 2017
43e195d
Clean up isGenericXXXType functions
ahejlsberg Dec 12, 2017
61225cc
Introduce TypeFlags.Instatiable
ahejlsberg Dec 13, 2017
9f74a7a
Rename TypeVariable to InstantiableType
ahejlsberg Dec 13, 2017
20434fa
Inference for conditional and extends type operators
ahejlsberg Dec 13, 2017
ddc631c
Fix typo
ahejlsberg Dec 13, 2017
000f121
Improve conditional type constraint checking
ahejlsberg Dec 13, 2017
27b945b
Handle constraints for distributive conditional types
ahejlsberg Dec 16, 2017
f59e2e6
Accept new baselines
ahejlsberg Dec 17, 2017
14590f1
Move JsxAttributes and MarkerType from TypeFlags to ObjectFlags
ahejlsberg Dec 18, 2017
100e4f6
Accept new baselines
ahejlsberg Dec 18, 2017
c5fd2f1
Parse xxx? as JSDoc type when not followed by token that starts type
ahejlsberg Dec 19, 2017
341c397
Accept new baselines
ahejlsberg Dec 19, 2017
3f4911f
Fix linting error
ahejlsberg Dec 20, 2017
abc8110
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 3, 2018
bb23bb2
Propagate both TypeFlags and ObjectFlags in getSpreadType
ahejlsberg Jan 3, 2018
c10a552
Eagerly evaluate S extends T when S is definitely or definitely not a…
ahejlsberg Jan 14, 2018
53b1572
Revert to extends check being part of conditional type
ahejlsberg Jan 15, 2018
5094f76
Remove 'T extends U' type constructor
ahejlsberg Jan 15, 2018
925da86
Accept new baselines
ahejlsberg Jan 15, 2018
e8d1740
Introduce substitution types to use for constrained type parameters
ahejlsberg Jan 15, 2018
15baf0e
Accept new baselines
ahejlsberg Jan 15, 2018
9598acd
Properly handle 'any' and 'never' as conditional check type
ahejlsberg Jan 15, 2018
e96ec8c
Erase substitution types in type references and type alias instantiat…
ahejlsberg Jan 16, 2018
d52fa71
Optimize the sameMap function
ahejlsberg Jan 16, 2018
4ec6fdd
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 17, 2018
fd0dd6e
Separate code path for conditional type instantiation
ahejlsberg Jan 18, 2018
c360c24
Fix parsing
ahejlsberg Jan 19, 2018
0e73240
Disallow conditional type following 'extends'
ahejlsberg Jan 19, 2018
5204fd5
Add T is related to { [P in xxx]: T[P] } type relationship
ahejlsberg Jan 20, 2018
eb314d0
Add tests
ahejlsberg Jan 20, 2018
cdd50d4
Accept new baselines
ahejlsberg Jan 20, 2018
fc7d1c3
Revise comments
ahejlsberg Jan 20, 2018
f19959a
Cache substitution types and remove erasure that was too eager
ahejlsberg Jan 20, 2018
b869290
Remove unnecessary caching of substitution types
ahejlsberg Jan 21, 2018
4c7ec3c
Shared code path for getConditionalType and instantiateConditionalType
ahejlsberg Jan 21, 2018
b42c6b1
Only conditional types that check naked type parameter distribute ove…
ahejlsberg Jan 24, 2018
8e337b5
Fix bug in resolveMappedTypeMembers
ahejlsberg Jan 24, 2018
4f2b5f3
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 30, 2018
f990e4e
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 30, 2018
01516c8
Update to use TypeFlags.Instantiable in instantiateSymbol
ahejlsberg Jan 30, 2018
d4dc67a
Merge branch 'master' into conditionalTypes
ahejlsberg Feb 3, 2018
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
Next Next commit
Initial implementation of conditional type operator
  • Loading branch information
ahejlsberg committed Dec 5, 2017
commit 57ca7680c9f5ec0d772c9dd94294cbcff2a29129
1 change: 1 addition & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3416,6 +3416,7 @@ namespace ts {
case SyntaxKind.TupleType:
case SyntaxKind.UnionType:
case SyntaxKind.IntersectionType:
case SyntaxKind.ConditionalType:
case SyntaxKind.ParenthesizedType:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
Expand Down
52 changes: 52 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2620,6 +2620,13 @@ namespace ts {
const indexTypeNode = typeToTypeNodeHelper((<IndexedAccessType>type).indexType, context);
return createIndexedAccessTypeNode(objectTypeNode, indexTypeNode);
}
if (type.flags & TypeFlags.Conditional) {
const checkTypeNode = typeToTypeNodeHelper((<ConditionalType>type).checkType, context);
const extendskTypeNode = typeToTypeNodeHelper((<ConditionalType>type).extendsType, context);
const trueTypeNode = typeToTypeNodeHelper((<ConditionalType>type).trueType, context);
const falseTypeNode = typeToTypeNodeHelper((<ConditionalType>type).falseType, context);
return createConditionalTypeNode(checkTypeNode, extendskTypeNode, trueTypeNode, falseTypeNode);
}

Debug.fail("Should be unreachable.");

Expand Down Expand Up @@ -3388,6 +3395,15 @@ namespace ts {
writeType((<IndexedAccessType>type).indexType, TypeFormatFlags.None);
writePunctuation(writer, SyntaxKind.CloseBracketToken);
}
else if (type.flags & TypeFlags.Conditional) {
writeType((<ConditionalType>type).checkType, TypeFormatFlags.InElementType);
writer.writeKeyword("extends");
writeType((<ConditionalType>type).extendsType, TypeFormatFlags.InElementType);
writePunctuation(writer, SyntaxKind.QuestionToken);
writeType((<ConditionalType>type).trueType, TypeFormatFlags.InElementType);
writePunctuation(writer, SyntaxKind.ColonToken);
writeType((<ConditionalType>type).falseType, TypeFormatFlags.InElementType);
}
else {
// Should never get here
// { ... }
Expand Down Expand Up @@ -8189,6 +8205,35 @@ namespace ts {
return links.resolvedType;
}

function getConditionalType(checkType: Type, extendsType: Type, trueType: Type, falseType: Type, aliasSymbol?: Symbol, aliasTypeArguments?: Type[]): Type {
if (checkType.flags & TypeFlags.Union) {
return getUnionType(map((<UnionType>checkType).types, t => getConditionalType(t, extendsType, trueType, falseType)),
/*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments);
}
if (isTypeAssignableTo(checkType, extendsType)) {
return trueType;
}
if (!isGenericObjectType(checkType) && !isGenericObjectType(extendsType)) {
return falseType;
}
const type = <ConditionalType>createType(TypeFlags.Conditional);
type.checkType = checkType;
type.extendsType = extendsType;
type.trueType = trueType;
type.falseType = falseType;
return type;
}

function getTypeFromConditionalTypeNode(node: ConditionalTypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
links.resolvedType = getConditionalType(getTypeFromTypeNode(node.checkType), getTypeFromTypeNode(node.extendsType),
getTypeFromTypeNode(node.trueType), getTypeFromTypeNode(node.falseType),
getAliasSymbolForTypeNode(node), getAliasTypeArgumentsForTypeNode(node));
}
return links.resolvedType;
}

function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node: TypeNode): Type {
const links = getNodeLinks(node);
if (!links.resolvedType) {
Expand Down Expand Up @@ -8481,6 +8526,8 @@ namespace ts {
return getTypeFromIndexedAccessTypeNode(<IndexedAccessTypeNode>node);
case SyntaxKind.MappedType:
return getTypeFromMappedTypeNode(<MappedTypeNode>node);
case SyntaxKind.ConditionalType:
return getTypeFromConditionalTypeNode(<ConditionalTypeNode>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:
Expand Down Expand Up @@ -8778,6 +8825,11 @@ namespace ts {
if (type.flags & TypeFlags.IndexedAccess) {
return getIndexedAccessType(instantiateType((<IndexedAccessType>type).objectType, mapper), instantiateType((<IndexedAccessType>type).indexType, mapper));
}
if (type.flags & TypeFlags.Conditional) {
return getConditionalType(instantiateType((<ConditionalType>type).checkType, mapper), instantiateType((<ConditionalType>type).extendsType, mapper),
instantiateType((<ConditionalType>type).trueType, mapper), instantiateType((<ConditionalType>type).falseType, mapper),
type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
}
}
return type;
}
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ namespace ts {
return emitUnionType(<UnionTypeNode>type);
case SyntaxKind.IntersectionType:
return emitIntersectionType(<IntersectionTypeNode>type);
case SyntaxKind.ConditionalType:
return emitConditionalType(<ConditionalTypeNode>type);
case SyntaxKind.ParenthesizedType:
return emitParenType(<ParenthesizedTypeNode>type);
case SyntaxKind.TypeOperator:
Expand Down Expand Up @@ -545,7 +547,17 @@ namespace ts {
emitSeparatedList(type.types, " & ", emitType);
}

function emitParenType(type: ParenthesizedTypeNode) {
function emitConditionalType(node: ConditionalTypeNode) {
emitType(node.checkType);
write(" extends ");
emitType(node.extendsType);
write(" ? ");
emitType(node.trueType);
write(" : ");
emitType(node.falseType);
}

function emitParenType(type: ParenthesizedTypeNode) {
write("(");
emitType(type.type);
write(")");
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ namespace ts {
return emitUnionType(<UnionTypeNode>node);
case SyntaxKind.IntersectionType:
return emitIntersectionType(<IntersectionTypeNode>node);
case SyntaxKind.ConditionalType:
return emitConditionalType(<ConditionalTypeNode>node);
case SyntaxKind.ParenthesizedType:
return emitParenthesizedType(<ParenthesizedTypeNode>node);
case SyntaxKind.ExpressionWithTypeArguments:
Expand Down Expand Up @@ -1129,6 +1131,16 @@ namespace ts {
emitList(node, node.types, ListFormat.IntersectionTypeConstituents);
}

function emitConditionalType(node: ConditionalTypeNode) {
emit(node.checkType);
write(" extends ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces should be written using writeSpace(), keywords using writeKeyword("extends"), and punctuation using writePunctuation("?") to support the symbol display builder integration with the emitter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else is written this way in emitter.ts. Are you saying it all needs to change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I was looking at the old declaration emitter. I will fix this.

emit(node.extendsType);
write(" ? ");
emit(node.trueType);
write(" : ");
emit(node.falseType);
}

function emitParenthesizedType(node: ParenthesizedTypeNode) {
write("(");
emit(node.type);
Expand Down
24 changes: 23 additions & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,24 @@ namespace ts {
: node;
}

export function createConditionalTypeNode(checkType: TypeNode, extendsType: TypeNode, trueType: TypeNode, falseType: TypeNode) {
const node = createSynthesizedNode(SyntaxKind.ConditionalType) as ConditionalTypeNode;
node.checkType = parenthesizeConditionalTypeMember(checkType);
node.extendsType = parenthesizeConditionalTypeMember(extendsType);
node.trueType = trueType;
node.falseType = falseType;
return node;
}

export function updateConditionalTypeNode(node: ConditionalTypeNode, checkType: TypeNode, extendsType: TypeNode, trueType: TypeNode, falseType: TypeNode) {
return node.checkType !== checkType
|| node.extendsType !== extendsType
|| node.trueType !== trueType
|| node.falseType !== falseType
? updateNode(createConditionalTypeNode(checkType, extendsType, trueType, falseType), node)
: node;
}

export function createParenthesizedType(type: TypeNode) {
const node = <ParenthesizedTypeNode>createSynthesizedNode(SyntaxKind.ParenthesizedType);
node.type = type;
Expand Down Expand Up @@ -4081,6 +4099,10 @@ namespace ts {
return expression;
}

export function parenthesizeConditionalTypeMember(member: TypeNode) {
return member.kind === SyntaxKind.ConditionalType ? createParenthesizedType(member) : member;
}

export function parenthesizeElementTypeMember(member: TypeNode) {
switch (member.kind) {
case SyntaxKind.UnionType:
Expand All @@ -4089,7 +4111,7 @@ namespace ts {
case SyntaxKind.ConstructorType:
return createParenthesizedType(member);
}
return member;
return parenthesizeConditionalTypeMember(member);
}

export function parenthesizeArrayTypeMember(member: TypeNode) {
Expand Down
26 changes: 25 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ namespace ts {
case SyntaxKind.UnionType:
case SyntaxKind.IntersectionType:
return visitNodes(cbNode, cbNodes, (<UnionOrIntersectionTypeNode>node).types);
case SyntaxKind.ConditionalType:
return visitNode(cbNode, (<ConditionalTypeNode>node).checkType) ||
visitNode(cbNode, (<ConditionalTypeNode>node).extendsType) ||
visitNode(cbNode, (<ConditionalTypeNode>node).trueType) ||
visitNode(cbNode, (<ConditionalTypeNode>node).falseType);
case SyntaxKind.ParenthesizedType:
case SyntaxKind.TypeOperator:
return visitNode(cbNode, (<ParenthesizedTypeNode | TypeOperatorNode>node).type);
Expand Down Expand Up @@ -2760,6 +2765,10 @@ namespace ts {
type = createJSDocPostfixType(SyntaxKind.JSDocNonNullableType, type);
break;
case SyntaxKind.QuestionToken:
// only parse postfix ? inside jsdoc, otherwise it is a conditional type
if (!(contextFlags & NodeFlags.JSDoc)) {
return type;
}
type = createJSDocPostfixType(SyntaxKind.JSDocNullableType, type);
break;
case SyntaxKind.OpenBracketToken:
Expand Down Expand Up @@ -2839,6 +2848,21 @@ namespace ts {
return parseUnionOrIntersectionType(SyntaxKind.UnionType, parseIntersectionTypeOrHigher, SyntaxKind.BarToken);
}

function parseConditionalTypeOrHigher(): TypeNode {
const type = parseUnionTypeOrHigher();
if (parseOptional(SyntaxKind.ExtendsKeyword)) {
const node = <ConditionalTypeNode>createNode(SyntaxKind.ConditionalType, type.pos);
node.checkType = type;
node.extendsType = parseUnionTypeOrHigher();
parseExpected(SyntaxKind.QuestionToken);
node.trueType = parseConditionalTypeOrHigher();
parseExpected(SyntaxKind.ColonToken);
node.falseType = parseConditionalTypeOrHigher();
return finishNode(node);
}
return type;
}

function isStartOfFunctionType(): boolean {
if (token() === SyntaxKind.LessThanToken) {
return true;
Expand Down Expand Up @@ -2928,7 +2952,7 @@ namespace ts {
if (token() === SyntaxKind.NewKeyword) {
return parseFunctionOrConstructorType(SyntaxKind.ConstructorType);
}
return parseUnionTypeOrHigher();
return parseConditionalTypeOrHigher();
}

function parseTypeAnnotation(): TypeNode {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ namespace ts {
case SyntaxKind.TypeReference:
case SyntaxKind.UnionType:
case SyntaxKind.IntersectionType:
case SyntaxKind.ConditionalType:
case SyntaxKind.ParenthesizedType:
case SyntaxKind.ThisType:
case SyntaxKind.TypeOperator:
Expand Down
37 changes: 27 additions & 10 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ namespace ts {
TupleType,
UnionType,
IntersectionType,
ConditionalType,
ParenthesizedType,
ThisType,
TypeOperator,
Expand Down Expand Up @@ -1065,6 +1066,14 @@ namespace ts {
types: NodeArray<TypeNode>;
}

export interface ConditionalTypeNode extends TypeNode {
kind: SyntaxKind.ConditionalType;
checkType: TypeNode;

@rbuckton rbuckton Jan 31, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still concerned that baking the S extends T head of a conditional type into ConditionalTypeNode would mean an API change to evolve conditional types to have other types of conditions in the future as that would affect tools that integrate TypeScript (like linters).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to speculate about what we might do here (i.e. nothing vs. additional conditions vs. discrete conditional operators), so I'm not keen on introducing more abstraction.

extendsType: TypeNode;
trueType: TypeNode;
falseType: TypeNode;
}

export interface ParenthesizedTypeNode extends TypeNode {
kind: SyntaxKind.ParenthesizedType;
type: TypeNode;
Expand Down Expand Up @@ -3339,18 +3348,19 @@ namespace ts {
Intersection = 1 << 18, // Intersection (T & U)
Index = 1 << 19, // keyof T
IndexedAccess = 1 << 20, // T[K]
Conditional = 1 << 21, // A extends B ? T : U
/* @internal */
FreshLiteral = 1 << 21, // Fresh literal or unique type
FreshLiteral = 1 << 22, // Fresh literal or unique type
/* @internal */
ContainsWideningType = 1 << 22, // Type is or contains undefined or null widening type
ContainsWideningType = 1 << 23, // Type is or contains undefined or null widening type
/* @internal */
ContainsObjectLiteral = 1 << 23, // Type is or contains object literal type
ContainsObjectLiteral = 1 << 24, // Type is or contains object literal type
/* @internal */
ContainsAnyFunctionType = 1 << 24, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 25, // intrinsic object type
ContainsAnyFunctionType = 1 << 25, // Type is or contains the anyFunctionType
NonPrimitive = 1 << 26, // intrinsic object type
/* @internal */
JsxAttributes = 1 << 26, // Jsx attributes type
MarkerType = 1 << 27, // Marker type used for variance probing
JsxAttributes = 1 << 27, // Jsx attributes type
MarkerType = 1 << 28, // Marker type used for variance probing

/* @internal */
Nullable = Undefined | Null,
Expand All @@ -3373,12 +3383,12 @@ namespace ts {
ESSymbolLike = ESSymbol | UniqueESSymbol,
UnionOrIntersection = Union | Intersection,
StructuredType = Object | Union | Intersection,
StructuredOrTypeVariable = StructuredType | TypeParameter | Index | IndexedAccess,
TypeVariable = TypeParameter | IndexedAccess,
TypeVariable = TypeParameter | IndexedAccess | Conditional,
StructuredOrTypeVariable = StructuredType | TypeVariable | Index,

// 'Narrowable' types are types where narrowing actually narrows.
// This *should* be every type other than null, undefined, void, and never
Narrowable = Any | StructuredType | TypeParameter | Index | IndexedAccess | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
Narrowable = Any | StructuredOrTypeVariable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
/* @internal */
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
Expand Down Expand Up @@ -3626,6 +3636,13 @@ namespace ts {
type: TypeVariable | UnionOrIntersectionType;
}

export interface ConditionalType extends TypeVariable {
checkType: Type;
extendsType: Type;
trueType: Type;
falseType: Type;
}

export const enum SignatureKind {
Call,
Construct,
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,10 @@ namespace ts {
return node.kind === SyntaxKind.IntersectionType;
}

export function isConditionalTypeNode(node: Node): node is ConditionalTypeNode {
return node.kind === SyntaxKind.ConditionalType;
}

export function isParenthesizedTypeNode(node: Node): node is ParenthesizedTypeNode {
return node.kind === SyntaxKind.ParenthesizedType;
}
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ namespace ts {
return updateIntersectionTypeNode(<IntersectionTypeNode>node,
nodesVisitor((<IntersectionTypeNode>node).types, visitor, isTypeNode));

case SyntaxKind.ConditionalType:
return updateConditionalTypeNode(<ConditionalTypeNode>node,
visitNode((<ConditionalTypeNode>node).checkType, visitor, isTypeNode),
visitNode((<ConditionalTypeNode>node).extendsType, visitor, isTypeNode),
visitNode((<ConditionalTypeNode>node).trueType, visitor, isTypeNode),
visitNode((<ConditionalTypeNode>node).falseType, visitor, isTypeNode));

case SyntaxKind.ParenthesizedType:
return updateParenthesizedType(<ParenthesizedTypeNode>node,
visitNode((<ParenthesizedTypeNode>node).type, visitor, isTypeNode));
Expand Down