Merge branch 'master' into compute-common-source-dir

This commit is contained in:
Wesley Wigham
2015-11-11 15:58:41 -08:00
55 changed files with 9461 additions and 6066 deletions
+1859 -1423
View File
File diff suppressed because it is too large Load Diff
+1057 -557
View File
File diff suppressed because it is too large Load Diff
+13 -6
View File
@@ -387,6 +387,7 @@ declare namespace ts {
right: Identifier;
}
type EntityName = Identifier | QualifiedName;
type PropertyName = Identifier | LiteralExpression | ComputedPropertyName;
type DeclarationName = Identifier | LiteralExpression | ComputedPropertyName | BindingPattern;
interface Declaration extends Node {
_declarationBrand: any;
@@ -425,7 +426,7 @@ declare namespace ts {
initializer?: Expression;
}
interface BindingElement extends Declaration {
propertyName?: Identifier;
propertyName?: PropertyName;
dotDotDotToken?: Node;
name: Identifier | BindingPattern;
initializer?: Expression;
@@ -452,7 +453,7 @@ declare namespace ts {
objectAssignmentInitializer?: Expression;
}
interface VariableLikeDeclaration extends Declaration {
propertyName?: Identifier;
propertyName?: PropertyName;
dotDotDotToken?: Node;
name: DeclarationName;
questionToken?: Node;
@@ -581,7 +582,7 @@ declare namespace ts {
asteriskToken?: Node;
expression?: Expression;
}
interface BinaryExpression extends Expression {
interface BinaryExpression extends Expression, Declaration {
left: Expression;
operatorToken: Node;
right: Expression;
@@ -625,7 +626,7 @@ declare namespace ts {
interface ObjectLiteralExpression extends PrimaryExpression, Declaration {
properties: NodeArray<ObjectLiteralElement>;
}
interface PropertyAccessExpression extends MemberExpression {
interface PropertyAccessExpression extends MemberExpression, Declaration {
expression: LeftHandSideExpression;
dotToken: Node;
name: Identifier;
@@ -1220,6 +1221,7 @@ declare namespace ts {
ObjectLiteral = 524288,
ESSymbol = 16777216,
ThisType = 33554432,
ObjectLiteralPatternWithComputedProperties = 67108864,
StringLike = 258,
NumberLike = 132,
ObjectType = 80896,
@@ -1537,7 +1539,6 @@ declare namespace ts {
function getTypeParameterOwner(d: Declaration): Declaration;
}
declare namespace ts {
function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node;
function createNode(kind: SyntaxKind, pos?: number, end?: number): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
@@ -2126,6 +2127,9 @@ declare namespace ts {
static typeAliasName: string;
static parameterName: string;
static docCommentTagName: string;
static jsxOpenTagName: string;
static jsxCloseTagName: string;
static jsxSelfClosingTagName: string;
}
enum ClassificationType {
comment = 1,
@@ -2146,6 +2150,9 @@ declare namespace ts {
typeAliasName = 16,
parameterName = 17,
docCommentTagName = 18,
jsxOpenTagName = 19,
jsxCloseTagName = 20,
jsxSelfClosingTagName = 21,
}
interface DisplayPartsSymbolWriter extends SymbolWriter {
displayParts(): SymbolDisplayPart[];
@@ -2171,7 +2178,7 @@ declare namespace ts {
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createGetCanonicalFileName(useCaseSensitivefileNames: boolean): (fileName: string) => string;
function createDocumentRegistry(useCaseSensitiveFileNames?: boolean, currentDirectory?: string): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function preProcessFile(sourceText: string, readImportFiles?: boolean, detectJavaScriptImports?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;
function createClassifier(): Classifier;
/**
+2546 -1958
View File
File diff suppressed because it is too large Load Diff
+13 -6
View File
@@ -387,6 +387,7 @@ declare namespace ts {
right: Identifier;
}
type EntityName = Identifier | QualifiedName;
type PropertyName = Identifier | LiteralExpression | ComputedPropertyName;
type DeclarationName = Identifier | LiteralExpression | ComputedPropertyName | BindingPattern;
interface Declaration extends Node {
_declarationBrand: any;
@@ -425,7 +426,7 @@ declare namespace ts {
initializer?: Expression;
}
interface BindingElement extends Declaration {
propertyName?: Identifier;
propertyName?: PropertyName;
dotDotDotToken?: Node;
name: Identifier | BindingPattern;
initializer?: Expression;
@@ -452,7 +453,7 @@ declare namespace ts {
objectAssignmentInitializer?: Expression;
}
interface VariableLikeDeclaration extends Declaration {
propertyName?: Identifier;
propertyName?: PropertyName;
dotDotDotToken?: Node;
name: DeclarationName;
questionToken?: Node;
@@ -581,7 +582,7 @@ declare namespace ts {
asteriskToken?: Node;
expression?: Expression;
}
interface BinaryExpression extends Expression {
interface BinaryExpression extends Expression, Declaration {
left: Expression;
operatorToken: Node;
right: Expression;
@@ -625,7 +626,7 @@ declare namespace ts {
interface ObjectLiteralExpression extends PrimaryExpression, Declaration {
properties: NodeArray<ObjectLiteralElement>;
}
interface PropertyAccessExpression extends MemberExpression {
interface PropertyAccessExpression extends MemberExpression, Declaration {
expression: LeftHandSideExpression;
dotToken: Node;
name: Identifier;
@@ -1220,6 +1221,7 @@ declare namespace ts {
ObjectLiteral = 524288,
ESSymbol = 16777216,
ThisType = 33554432,
ObjectLiteralPatternWithComputedProperties = 67108864,
StringLike = 258,
NumberLike = 132,
ObjectType = 80896,
@@ -1537,7 +1539,6 @@ declare namespace ts {
function getTypeParameterOwner(d: Declaration): Declaration;
}
declare namespace ts {
function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node;
function createNode(kind: SyntaxKind, pos?: number, end?: number): Node;
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
@@ -2126,6 +2127,9 @@ declare namespace ts {
static typeAliasName: string;
static parameterName: string;
static docCommentTagName: string;
static jsxOpenTagName: string;
static jsxCloseTagName: string;
static jsxSelfClosingTagName: string;
}
enum ClassificationType {
comment = 1,
@@ -2146,6 +2150,9 @@ declare namespace ts {
typeAliasName = 16,
parameterName = 17,
docCommentTagName = 18,
jsxOpenTagName = 19,
jsxCloseTagName = 20,
jsxSelfClosingTagName = 21,
}
interface DisplayPartsSymbolWriter extends SymbolWriter {
displayParts(): SymbolDisplayPart[];
@@ -2171,7 +2178,7 @@ declare namespace ts {
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
function createGetCanonicalFileName(useCaseSensitivefileNames: boolean): (fileName: string) => string;
function createDocumentRegistry(useCaseSensitiveFileNames?: boolean, currentDirectory?: string): DocumentRegistry;
function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo;
function preProcessFile(sourceText: string, readImportFiles?: boolean, detectJavaScriptImports?: boolean): PreProcessedFileInfo;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService;
function createClassifier(): Classifier;
/**
+2546 -1958
View File
File diff suppressed because it is too large Load Diff
+113 -101
View File
@@ -81,7 +81,7 @@ namespace ts {
symbolToString,
getAugmentedPropertiesOfType,
getRootSymbols,
getContextualType: getApparentTypeOfContextualType,
getContextualType,
getFullyQualifiedName,
getResolvedSignature,
getConstantValue,
@@ -109,9 +109,9 @@ namespace ts {
const undefinedType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined");
const nullType = createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsUndefinedOrNull, "null");
const unknownType = createIntrinsicType(TypeFlags.Any, "unknown");
const circularType = createIntrinsicType(TypeFlags.Any, "__circular__");
const emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
const emptyUnionType = emptyObjectType;
const emptyGenericType = <GenericType><ObjectType>createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined);
emptyGenericType.instantiations = {};
@@ -486,9 +486,19 @@ namespace ts {
if (location.kind === SyntaxKind.SourceFile ||
(location.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>location).name.kind === SyntaxKind.StringLiteral)) {
// It's an external module. Because of module/namespace merging, a module's exports are in scope,
// yet we never want to treat an export specifier as putting a member in scope. Therefore,
// if the name we find is purely an export specifier, it is not actually considered in scope.
// It's an external module. First see if the module has an export default and if the local
// name of that export default matches.
if (result = moduleExports["default"]) {
const localSymbol = getLocalSymbolForExportDefault(result);
if (localSymbol && (result.flags & meaning) && localSymbol.name === name) {
break loop;
}
result = undefined;
}
// Because of module/namespace merging, a module's exports are in scope,
// yet we never want to treat an export specifier as putting a member in scope.
// Therefore, if the name we find is purely an export specifier, it is not actually considered in scope.
// Two things to note about this:
// 1. We have to check this without calling getSymbol. The problem with calling getSymbol
// on an export specifier is that it might find the export specifier itself, and try to
@@ -502,13 +512,6 @@ namespace ts {
getDeclarationOfKind(moduleExports[name], SyntaxKind.ExportSpecifier)) {
break;
}
result = moduleExports["default"];
const localSymbol = getLocalSymbolForExportDefault(result);
if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) {
break loop;
}
result = undefined;
}
if (result = getSymbol(moduleExports, name, meaning & SymbolFlags.ModuleMember)) {
@@ -3796,7 +3799,7 @@ namespace ts {
if (node.initializer) {
const signatureDeclaration = <SignatureDeclaration>node.parent;
const signature = getSignatureFromDeclaration(signatureDeclaration);
const parameterIndex = signatureDeclaration.parameters.indexOf(node);
const parameterIndex = ts.indexOf(signatureDeclaration.parameters, node);
Debug.assert(parameterIndex >= 0);
return parameterIndex >= signature.minArgumentCount;
}
@@ -4201,12 +4204,12 @@ namespace ts {
// We only support expressions that are simple qualified names. For other expressions this produces undefined.
const typeNameOrExpression = node.kind === SyntaxKind.TypeReference ? (<TypeReferenceNode>node).typeName :
isSupportedExpressionWithTypeArguments(<ExpressionWithTypeArguments>node) ? (<ExpressionWithTypeArguments>node).expression :
undefined;
undefined;
const symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, SymbolFlags.Type) || unknownSymbol;
const type = symbol === unknownSymbol ? unknownType :
symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface) ? getTypeFromClassOrInterfaceReference(node, symbol) :
symbol.flags & SymbolFlags.TypeAlias ? getTypeFromTypeAliasReference(node, symbol) :
getTypeFromNonGenericTypeReference(node, symbol);
symbol.flags & SymbolFlags.TypeAlias ? getTypeFromTypeAliasReference(node, symbol) :
getTypeFromNonGenericTypeReference(node, symbol);
// Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the
// type reference in checkTypeReferenceOrExpressionWithTypeArguments.
links.resolvedSymbol = symbol;
@@ -4410,7 +4413,7 @@ namespace ts {
// a named type that circularly references itself.
function getUnionType(types: Type[], noSubtypeReduction?: boolean): Type {
if (types.length === 0) {
return emptyObjectType;
return emptyUnionType;
}
const typeSet: Type[] = [];
addTypesToSet(typeSet, types, TypeFlags.Union);
@@ -6282,27 +6285,6 @@ namespace ts {
Debug.fail("should not get here");
}
// For a union type, remove all constituent types that are of the given type kind (when isOfTypeKind is true)
// or not of the given type kind (when isOfTypeKind is false)
function removeTypesFromUnionType(type: Type, typeKind: TypeFlags, isOfTypeKind: boolean, allowEmptyUnionResult: boolean): Type {
if (type.flags & TypeFlags.Union) {
const types = (<UnionType>type).types;
if (forEach(types, t => !!(t.flags & typeKind) === isOfTypeKind)) {
// Above we checked if we have anything to remove, now use the opposite test to do the removal
const narrowedType = getUnionType(filter(types, t => !(t.flags & typeKind) === isOfTypeKind));
if (allowEmptyUnionResult || narrowedType !== emptyObjectType) {
return narrowedType;
}
}
}
else if (allowEmptyUnionResult && !!(type.flags & typeKind) === isOfTypeKind) {
// Use getUnionType(emptyArray) instead of emptyObjectType in case the way empty union types
// are represented ever changes.
return getUnionType(emptyArray);
}
return type;
}
function hasInitializer(node: VariableLikeDeclaration): boolean {
return !!(node.initializer || isBindingPattern(node.parent) && hasInitializer(<VariableLikeDeclaration>node.parent.parent));
}
@@ -6404,33 +6386,16 @@ namespace ts {
// Only narrow when symbol is variable of type any or an object, union, or type parameter type
if (node && symbol.flags & SymbolFlags.Variable) {
if (isTypeAny(type) || type.flags & (TypeFlags.ObjectType | TypeFlags.Union | TypeFlags.TypeParameter)) {
const originalType = type;
const nodeStack: {node: Node, child: Node}[] = [];
loop: while (node.parent) {
const child = node;
node = node.parent;
let narrowedType = type;
switch (node.kind) {
case SyntaxKind.IfStatement:
// In a branch of an if statement, narrow based on controlling expression
if (child !== (<IfStatement>node).expression) {
narrowedType = narrowType(type, (<IfStatement>node).expression, /*assumeTrue*/ child === (<IfStatement>node).thenStatement);
}
break;
case SyntaxKind.ConditionalExpression:
// In a branch of a conditional expression, narrow based on controlling condition
if (child !== (<ConditionalExpression>node).condition) {
narrowedType = narrowType(type, (<ConditionalExpression>node).condition, /*assumeTrue*/ child === (<ConditionalExpression>node).whenTrue);
}
break;
case SyntaxKind.BinaryExpression:
// In the right operand of an && or ||, narrow based on left operand
if (child === (<BinaryExpression>node).right) {
if ((<BinaryExpression>node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) {
narrowedType = narrowType(type, (<BinaryExpression>node).left, /*assumeTrue*/ true);
}
else if ((<BinaryExpression>node).operatorToken.kind === SyntaxKind.BarBarToken) {
narrowedType = narrowType(type, (<BinaryExpression>node).left, /*assumeTrue*/ false);
}
}
nodeStack.push({node, child});
break;
case SyntaxKind.SourceFile:
case SyntaxKind.ModuleDeclaration:
@@ -6443,13 +6408,48 @@ namespace ts {
// Stop at the first containing function or module declaration
break loop;
}
// Use narrowed type if construct contains no assignments to variable
if (narrowedType !== type) {
if (isVariableAssignedWithin(symbol, node)) {
}
let nodes: {node: Node, child: Node};
while (nodes = nodeStack.pop()) {
const {node, child} = nodes;
switch (node.kind) {
case SyntaxKind.IfStatement:
// In a branch of an if statement, narrow based on controlling expression
if (child !== (<IfStatement>node).expression) {
type = narrowType(type, (<IfStatement>node).expression, /*assumeTrue*/ child === (<IfStatement>node).thenStatement);
}
break;
}
type = narrowedType;
case SyntaxKind.ConditionalExpression:
// In a branch of a conditional expression, narrow based on controlling condition
if (child !== (<ConditionalExpression>node).condition) {
type = narrowType(type, (<ConditionalExpression>node).condition, /*assumeTrue*/ child === (<ConditionalExpression>node).whenTrue);
}
break;
case SyntaxKind.BinaryExpression:
// In the right operand of an && or ||, narrow based on left operand
if (child === (<BinaryExpression>node).right) {
if ((<BinaryExpression>node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) {
type = narrowType(type, (<BinaryExpression>node).left, /*assumeTrue*/ true);
}
else if ((<BinaryExpression>node).operatorToken.kind === SyntaxKind.BarBarToken) {
type = narrowType(type, (<BinaryExpression>node).left, /*assumeTrue*/ false);
}
}
break;
default:
Debug.fail("Unreachable!");
}
// Use original type if construct contains assignments to variable
if (type !== originalType && isVariableAssignedWithin(symbol, node)) {
type = originalType;
}
}
// Preserve old top-level behavior - if the branch is really an empty set, revert to prior type
if (type === emptyUnionType) {
type = originalType;
}
}
}
@@ -6466,31 +6466,31 @@ namespace ts {
if (left.expression.kind !== SyntaxKind.Identifier || getResolvedSymbol(<Identifier>left.expression) !== symbol) {
return type;
}
const typeInfo = primitiveTypeInfo[right.text];
if (expr.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken) {
assumeTrue = !assumeTrue;
}
if (assumeTrue) {
// Assumed result is true. If check was not for a primitive type, remove all primitive types
if (!typeInfo) {
return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean | TypeFlags.ESSymbol,
/*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ false);
}
// Check was for a primitive type, return that primitive type if it is a subtype
if (isTypeSubtypeOf(typeInfo.type, type)) {
return typeInfo.type;
}
// Otherwise, remove all types that aren't of the primitive type kind. This can happen when the type is
// union of enum types and other types.
return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ false, /*allowEmptyUnionResult*/ false);
const typeInfo = primitiveTypeInfo[right.text];
// If the type to be narrowed is any and we're checking a primitive with assumeTrue=true, return the primitive
if (!!(type.flags & TypeFlags.Any) && typeInfo && assumeTrue) {
return typeInfo.type;
}
let flags: TypeFlags;
if (typeInfo) {
flags = typeInfo.flags;
}
else {
// Assumed result is false. If check was for a primitive type, remove that primitive type
if (typeInfo) {
return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ false);
}
// Otherwise we don't have enough information to do anything.
return type;
assumeTrue = !assumeTrue;
flags = TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol | TypeFlags.Boolean;
}
// At this point we can bail if it's not a union
if (!(type.flags & TypeFlags.Union)) {
// If the active non-union type would be removed from a union by this type guard, return an empty union
return filterUnion(type) ? type : emptyUnionType;
}
return getUnionType(filter((type as UnionType).types, filterUnion), /*noSubtypeReduction*/ true);
function filterUnion(type: Type) {
return assumeTrue === !!(type.flags & flags);
}
}
@@ -6504,7 +6504,7 @@ namespace ts {
// and the second operand was false. We narrow with those assumptions and union the two resulting types.
return getUnionType([
narrowType(type, expr.left, /*assumeTrue*/ false),
narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ false)
narrowType(type, expr.right, /*assumeTrue*/ false)
]);
}
}
@@ -6515,7 +6515,7 @@ namespace ts {
// and the second operand was true. We narrow with those assumptions and union the two resulting types.
return getUnionType([
narrowType(type, expr.left, /*assumeTrue*/ true),
narrowType(narrowType(type, expr.left, /*assumeTrue*/ false), expr.right, /*assumeTrue*/ true)
narrowType(type, expr.right, /*assumeTrue*/ true)
]);
}
else {
@@ -7058,7 +7058,7 @@ namespace ts {
else if (operator === SyntaxKind.BarBarToken) {
// When an || expression has a contextual type, the operands are contextually typed by that type. When an ||
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
let type = getApparentTypeOfContextualType(binaryExpression);
let type = getContextualType(binaryExpression);
if (!type && node === binaryExpression.right) {
type = checkExpression(binaryExpression.left);
}
@@ -7173,7 +7173,7 @@ namespace ts {
// In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
function getContextualTypeForConditionalOperand(node: Expression): Type {
const conditional = <ConditionalExpression>node.parent;
return node === conditional.whenTrue || node === conditional.whenFalse ? getApparentTypeOfContextualType(conditional) : undefined;
return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined;
}
function getContextualTypeForJsxExpression(expr: JsxExpression | JsxSpreadAttribute): Type {
@@ -7206,9 +7206,16 @@ namespace ts {
/**
* Woah! Do you really want to use this function?
*
* Unless you're trying to get the *non-apparent* type for a value-literal type,
* Unless you're trying to get the *non-apparent* type for a
* value-literal type or you're authoring relevant portions of this algorithm,
* you probably meant to use 'getApparentTypeOfContextualType'.
* Otherwise this is slightly less useful.
* Otherwise this may not be very useful.
*
* In cases where you *are* working on this function, you should understand
* when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContetxualType'.
*
* - Use 'getContextualType' when you are simply going to propagate the result to the expression.
* - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type.
*
* @param node the expression whose contextual type will be returned.
* @returns the contextual type of an expression.
@@ -7252,7 +7259,7 @@ namespace ts {
Debug.assert(parent.parent.kind === SyntaxKind.TemplateExpression);
return getContextualTypeForSubstitutionExpression(<TemplateExpression>parent.parent, node);
case SyntaxKind.ParenthesizedExpression:
return getApparentTypeOfContextualType(<ParenthesizedExpression>parent);
return getContextualType(<ParenthesizedExpression>parent);
case SyntaxKind.JsxExpression:
case SyntaxKind.JsxSpreadAttribute:
return getContextualTypeForJsxExpression(<JsxExpression>parent);
@@ -11439,7 +11446,7 @@ namespace ts {
// Abstract methods can't have an implementation -- in particular, they don't need one.
if (!isExportSymbolInsideModule && lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body &&
!(lastSeenNonAmbientDeclaration.flags & NodeFlags.Abstract) ) {
!(lastSeenNonAmbientDeclaration.flags & NodeFlags.Abstract)) {
reportImplementationExpectedError(lastSeenNonAmbientDeclaration);
}
@@ -12726,9 +12733,14 @@ namespace ts {
// After we remove all types that are StringLike, we will know if there was a string constituent
// based on whether the remaining type is the same as the initial type.
const arrayType = removeTypesFromUnionType(arrayOrStringType, TypeFlags.StringLike, /*isTypeOfKind*/ true, /*allowEmptyUnionResult*/ true);
let arrayType = arrayOrStringType;
if (arrayOrStringType.flags & TypeFlags.Union) {
arrayType = getUnionType(filter((arrayOrStringType as UnionType).types, t => !(t.flags & TypeFlags.StringLike)));
}
else if (arrayOrStringType.flags & TypeFlags.StringLike) {
arrayType = emptyUnionType;
}
const hasStringConstituent = arrayOrStringType !== arrayType;
let reportedError = false;
if (hasStringConstituent) {
if (languageVersion < ScriptTarget.ES5) {
@@ -14373,8 +14385,8 @@ namespace ts {
if (className) {
copySymbol(location.symbol, meaning);
}
// fall through; this fall-through is necessary because we would like to handle
// type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration
// fall through; this fall-through is necessary because we would like to handle
// type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
// If we didn't come from static member of class or interface,
@@ -14530,8 +14542,8 @@ namespace ts {
return resolveEntityName(<EntityName>entityName, meaning);
}
else if ((entityName.parent.kind === SyntaxKind.JsxOpeningElement) ||
(entityName.parent.kind === SyntaxKind.JsxSelfClosingElement) ||
(entityName.parent.kind === SyntaxKind.JsxClosingElement)) {
(entityName.parent.kind === SyntaxKind.JsxSelfClosingElement) ||
(entityName.parent.kind === SyntaxKind.JsxClosingElement)) {
return getJsxElementTagSymbol(<JsxOpeningLikeElement>entityName.parent);
}
else if (isExpression(entityName)) {
@@ -14598,8 +14610,8 @@ namespace ts {
: getSymbolOfPartOfRightHandSideOfImportEquals(<Identifier>node);
}
else if (node.parent.kind === SyntaxKind.BindingElement &&
node.parent.parent.kind === SyntaxKind.ObjectBindingPattern &&
node === (<BindingElement>node.parent).propertyName) {
node.parent.parent.kind === SyntaxKind.ObjectBindingPattern &&
node === (<BindingElement>node.parent).propertyName) {
const typeOfPattern = getTypeOfNode(node.parent.parent);
const propertyDeclaration = typeOfPattern && getPropertyOfType(typeOfPattern, (<Identifier>node).text);
@@ -14636,7 +14648,7 @@ namespace ts {
(<ImportDeclaration>node.parent).moduleSpecifier === node)) {
return resolveExternalModuleName(node, <LiteralExpression>node);
}
// Fall through
// Fall through
case SyntaxKind.NumericLiteral:
// index access
@@ -14832,7 +14844,7 @@ namespace ts {
if (links.isNestedRedeclaration === undefined) {
const container = getEnclosingBlockScopeContainer(symbol.valueDeclaration);
links.isNestedRedeclaration = isStatementWithLocals(container) &&
!!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
!!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
}
return links.isNestedRedeclaration;
}
+11
View File
@@ -346,9 +346,20 @@ namespace ts {
return;
}
}
if (!cachedConfigFileText) {
const error = createCompilerDiagnostic(Diagnostics.File_0_not_found, configFileName);
reportDiagnostics([error], /* compilerHost */ undefined);
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
return;
}
const result = parseConfigFileTextToJson(configFileName, cachedConfigFileText);
const configObject = result.config;
if (!configObject) {
reportDiagnostics([result.error], /* compilerHost */ undefined);
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
return;
}
const configParseResult = parseJsonConfigFileContent(configObject, sys, getDirectoryPath(configFileName));
if (configParseResult.errors.length > 0) {
reportDiagnostics(configParseResult.errors, /* compilerHost */ undefined);
+2 -2
View File
@@ -14,7 +14,7 @@ namespace ts.formatting {
constructor(from: SyntaxKind, to: SyntaxKind, except: SyntaxKind[]) {
this.tokens = [];
for (let token = from; token <= to; token++) {
if (except.indexOf(token) < 0) {
if (ts.indexOf(except, token) < 0) {
this.tokens.push(token);
}
}
@@ -123,4 +123,4 @@ namespace ts.formatting {
static TypeNames = TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.NumberKeyword, SyntaxKind.StringKeyword, SyntaxKind.BooleanKeyword, SyntaxKind.SymbolKeyword, SyntaxKind.VoidKeyword, SyntaxKind.AnyKeyword]);
}
}
}
}
+26
View File
@@ -1612,6 +1612,9 @@ namespace ts {
public static typeAliasName = "type alias name";
public static parameterName = "parameter name";
public static docCommentTagName = "doc comment tag name";
public static jsxOpenTagName = "jsx open tag name";
public static jsxCloseTagName = "jsx close tag name";
public static jsxSelfClosingTagName = "jsx self closing tag name";
}
export const enum ClassificationType {
@@ -1633,6 +1636,9 @@ namespace ts {
typeAliasName = 16,
parameterName = 17,
docCommentTagName = 18,
jsxOpenTagName = 19,
jsxCloseTagName = 20,
jsxSelfClosingTagName = 21,
}
/// Language Service
@@ -6710,6 +6716,9 @@ namespace ts {
case ClassificationType.typeAliasName: return ClassificationTypeNames.typeAliasName;
case ClassificationType.parameterName: return ClassificationTypeNames.parameterName;
case ClassificationType.docCommentTagName: return ClassificationTypeNames.docCommentTagName;
case ClassificationType.jsxOpenTagName: return ClassificationTypeNames.jsxOpenTagName;
case ClassificationType.jsxCloseTagName: return ClassificationTypeNames.jsxCloseTagName;
case ClassificationType.jsxSelfClosingTagName: return ClassificationTypeNames.jsxSelfClosingTagName;
}
}
@@ -7022,6 +7031,23 @@ namespace ts {
}
return;
case SyntaxKind.JsxOpeningElement:
if ((<JsxOpeningElement>token.parent).tagName === token) {
return ClassificationType.jsxOpenTagName;
}
return;
case SyntaxKind.JsxClosingElement:
if ((<JsxClosingElement>token.parent).tagName === token) {
return ClassificationType.jsxCloseTagName;
}
return;
case SyntaxKind.JsxSelfClosingElement:
if ((<JsxSelfClosingElement>token.parent).tagName === token) {
return ClassificationType.jsxSelfClosingTagName;
}
return;
}
}
@@ -0,0 +1,27 @@
//// [tests/cases/conformance/es6/modules/reExportDefaultExport.ts] ////
//// [m1.ts]
export default function f() {
}
export {f};
//// [m2.ts]
import foo from "./m1";
import {f} from "./m1";
f();
foo();
//// [m1.js]
function f() {
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = f;
exports.f = f;
//// [m2.js]
var m1_1 = require("./m1");
var m1_2 = require("./m1");
m1_2.f();
m1_1.default();
@@ -0,0 +1,22 @@
=== tests/cases/conformance/es6/modules/m1.ts ===
export default function f() {
>f : Symbol(f, Decl(m1.ts, 0, 0))
}
export {f};
>f : Symbol(f, Decl(m1.ts, 3, 8))
=== tests/cases/conformance/es6/modules/m2.ts ===
import foo from "./m1";
>foo : Symbol(foo, Decl(m2.ts, 0, 6))
import {f} from "./m1";
>f : Symbol(f, Decl(m2.ts, 1, 8))
f();
>f : Symbol(f, Decl(m2.ts, 1, 8))
foo();
>foo : Symbol(foo, Decl(m2.ts, 0, 6))
@@ -0,0 +1,24 @@
=== tests/cases/conformance/es6/modules/m1.ts ===
export default function f() {
>f : () => void
}
export {f};
>f : () => void
=== tests/cases/conformance/es6/modules/m2.ts ===
import foo from "./m1";
>foo : () => void
import {f} from "./m1";
>f : () => void
f();
>f() : void
>f : () => void
foo();
>foo() : void
>foo : () => void
@@ -0,0 +1,26 @@
//// [stringLiteralTypesAndLogicalOrExpressions01.ts]
declare function myRandBool(): boolean;
let a: "foo" = "foo";
let b = a || "foo";
let c: "foo" = b;
let d = b || "bar";
let e: "foo" | "bar" = d;
//// [stringLiteralTypesAndLogicalOrExpressions01.js]
var a = "foo";
var b = a || "foo";
var c = b;
var d = b || "bar";
var e = d;
//// [stringLiteralTypesAndLogicalOrExpressions01.d.ts]
declare function myRandBool(): boolean;
declare let a: "foo";
declare let b: "foo";
declare let c: "foo";
declare let d: "foo" | "bar";
declare let e: "foo" | "bar";
@@ -0,0 +1,24 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndLogicalOrExpressions01.ts ===
declare function myRandBool(): boolean;
>myRandBool : Symbol(myRandBool, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 0, 0))
let a: "foo" = "foo";
>a : Symbol(a, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 3, 3))
let b = a || "foo";
>b : Symbol(b, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 4, 3))
>a : Symbol(a, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 3, 3))
let c: "foo" = b;
>c : Symbol(c, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 5, 3))
>b : Symbol(b, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 4, 3))
let d = b || "bar";
>d : Symbol(d, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 6, 3))
>b : Symbol(b, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 4, 3))
let e: "foo" | "bar" = d;
>e : Symbol(e, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 7, 3))
>d : Symbol(d, Decl(stringLiteralTypesAndLogicalOrExpressions01.ts, 6, 3))
@@ -0,0 +1,29 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndLogicalOrExpressions01.ts ===
declare function myRandBool(): boolean;
>myRandBool : () => boolean
let a: "foo" = "foo";
>a : "foo"
>"foo" : "foo"
let b = a || "foo";
>b : "foo"
>a || "foo" : "foo"
>a : "foo"
>"foo" : "foo"
let c: "foo" = b;
>c : "foo"
>b : "foo"
let d = b || "bar";
>d : "foo" | "bar"
>b || "bar" : "foo" | "bar"
>b : "foo"
>"bar" : "bar"
let e: "foo" | "bar" = d;
>e : "foo" | "bar"
>d : "foo" | "bar"
@@ -0,0 +1,23 @@
//// [stringLiteralTypesAndParenthesizedExpressions01.ts]
declare function myRandBool(): boolean;
let a: "foo" = ("foo");
let b: "foo" | "bar" = ("foo");
let c: "foo" = (myRandBool ? "foo" : ("foo"));
let d: "foo" | "bar" = (myRandBool ? "foo" : ("bar"));
//// [stringLiteralTypesAndParenthesizedExpressions01.js]
var a = ("foo");
var b = ("foo");
var c = (myRandBool ? "foo" : ("foo"));
var d = (myRandBool ? "foo" : ("bar"));
//// [stringLiteralTypesAndParenthesizedExpressions01.d.ts]
declare function myRandBool(): boolean;
declare let a: "foo";
declare let b: "foo" | "bar";
declare let c: "foo";
declare let d: "foo" | "bar";
@@ -0,0 +1,19 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndParenthesizedExpressions01.ts ===
declare function myRandBool(): boolean;
>myRandBool : Symbol(myRandBool, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 0, 0))
let a: "foo" = ("foo");
>a : Symbol(a, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 3, 3))
let b: "foo" | "bar" = ("foo");
>b : Symbol(b, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 4, 3))
let c: "foo" = (myRandBool ? "foo" : ("foo"));
>c : Symbol(c, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 5, 3))
>myRandBool : Symbol(myRandBool, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 0, 0))
let d: "foo" | "bar" = (myRandBool ? "foo" : ("bar"));
>d : Symbol(d, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 6, 3))
>myRandBool : Symbol(myRandBool, Decl(stringLiteralTypesAndParenthesizedExpressions01.ts, 0, 0))
@@ -0,0 +1,33 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndParenthesizedExpressions01.ts ===
declare function myRandBool(): boolean;
>myRandBool : () => boolean
let a: "foo" = ("foo");
>a : "foo"
>("foo") : "foo"
>"foo" : "foo"
let b: "foo" | "bar" = ("foo");
>b : "foo" | "bar"
>("foo") : "foo"
>"foo" : "foo"
let c: "foo" = (myRandBool ? "foo" : ("foo"));
>c : "foo"
>(myRandBool ? "foo" : ("foo")) : "foo"
>myRandBool ? "foo" : ("foo") : "foo"
>myRandBool : () => boolean
>"foo" : "foo"
>("foo") : "foo"
>"foo" : "foo"
let d: "foo" | "bar" = (myRandBool ? "foo" : ("bar"));
>d : "foo" | "bar"
>(myRandBool ? "foo" : ("bar")) : "foo" | "bar"
>myRandBool ? "foo" : ("bar") : "foo" | "bar"
>myRandBool : () => boolean
>"foo" : "foo"
>("bar") : "bar"
>"bar" : "bar"
@@ -0,0 +1,68 @@
//// [stringLiteralTypesAsTypeParameterConstraint01.ts]
function foo<T extends "foo">(f: (x: T) => T) {
return f;
}
function bar<T extends "foo" | "bar">(f: (x: T) => T) {
return f;
}
let f = foo(x => x);
let fResult = f("foo");
let g = foo((x => x));
let gResult = g("foo");
let h = bar(x => x);
let hResult = h("foo");
hResult = h("bar");
//// [stringLiteralTypesAsTypeParameterConstraint01.js]
function foo(f) {
return f;
}
function bar(f) {
return f;
}
var f = foo(function (x) { return x; });
var fResult = f("foo");
var g = foo((function (x) { return x; }));
var gResult = g("foo");
var h = bar(function (x) { return x; });
var hResult = h("foo");
hResult = h("bar");
//// [stringLiteralTypesAsTypeParameterConstraint01.d.ts]
declare function foo<T extends "foo">(f: (x: T) => T): (x: T) => T;
declare function bar<T extends "foo" | "bar">(f: (x: T) => T): (x: T) => T;
declare let f: (x: "foo") => "foo";
declare let fResult: "foo";
declare let g: (x: "foo") => "foo";
declare let gResult: "foo";
declare let h: (x: "foo" | "bar") => "foo" | "bar";
declare let hResult: "foo" | "bar";
//// [DtsFileErrors]
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint01.d.ts(3,16): error TS2382: Specialized overload signature is not assignable to any non-specialized signature.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint01.d.ts(5,16): error TS2382: Specialized overload signature is not assignable to any non-specialized signature.
==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint01.d.ts (2 errors) ====
declare function foo<T extends "foo">(f: (x: T) => T): (x: T) => T;
declare function bar<T extends "foo" | "bar">(f: (x: T) => T): (x: T) => T;
declare let f: (x: "foo") => "foo";
~~~~~~~~~~~~~~~~~~~
!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature.
declare let fResult: "foo";
declare let g: (x: "foo") => "foo";
~~~~~~~~~~~~~~~~~~~
!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature.
declare let gResult: "foo";
declare let h: (x: "foo" | "bar") => "foo" | "bar";
declare let hResult: "foo" | "bar";
@@ -0,0 +1,60 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint01.ts ===
function foo<T extends "foo">(f: (x: T) => T) {
>foo : Symbol(foo, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 0, 0))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 13))
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 30))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 34))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 13))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 13))
return f;
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 1, 30))
}
function bar<T extends "foo" | "bar">(f: (x: T) => T) {
>bar : Symbol(bar, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 3, 1))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 13))
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 38))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 42))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 13))
>T : Symbol(T, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 13))
return f;
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 5, 38))
}
let f = foo(x => x);
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 9, 3))
>foo : Symbol(foo, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 0, 0))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 9, 12))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 9, 12))
let fResult = f("foo");
>fResult : Symbol(fResult, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 10, 3))
>f : Symbol(f, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 9, 3))
let g = foo((x => x));
>g : Symbol(g, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 12, 3))
>foo : Symbol(foo, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 0, 0))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 12, 13))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 12, 13))
let gResult = g("foo");
>gResult : Symbol(gResult, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 13, 3))
>g : Symbol(g, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 12, 3))
let h = bar(x => x);
>h : Symbol(h, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 15, 3))
>bar : Symbol(bar, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 3, 1))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 15, 12))
>x : Symbol(x, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 15, 12))
let hResult = h("foo");
>hResult : Symbol(hResult, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 16, 3))
>h : Symbol(h, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 15, 3))
hResult = h("bar");
>hResult : Symbol(hResult, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 16, 3))
>h : Symbol(h, Decl(stringLiteralTypesAsTypeParameterConstraint01.ts, 15, 3))
@@ -0,0 +1,76 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint01.ts ===
function foo<T extends "foo">(f: (x: T) => T) {
>foo : <T extends "foo">(f: (x: T) => T) => (x: T) => T
>T : T
>f : (x: T) => T
>x : T
>T : T
>T : T
return f;
>f : (x: T) => T
}
function bar<T extends "foo" | "bar">(f: (x: T) => T) {
>bar : <T extends "foo" | "bar">(f: (x: T) => T) => (x: T) => T
>T : T
>f : (x: T) => T
>x : T
>T : T
>T : T
return f;
>f : (x: T) => T
}
let f = foo(x => x);
>f : (x: "foo") => "foo"
>foo(x => x) : (x: "foo") => "foo"
>foo : <T extends "foo">(f: (x: T) => T) => (x: T) => T
>x => x : (x: "foo") => "foo"
>x : "foo"
>x : "foo"
let fResult = f("foo");
>fResult : "foo"
>f("foo") : "foo"
>f : (x: "foo") => "foo"
>"foo" : "foo"
let g = foo((x => x));
>g : (x: "foo") => "foo"
>foo((x => x)) : (x: "foo") => "foo"
>foo : <T extends "foo">(f: (x: T) => T) => (x: T) => T
>(x => x) : (x: "foo") => "foo"
>x => x : (x: "foo") => "foo"
>x : "foo"
>x : "foo"
let gResult = g("foo");
>gResult : "foo"
>g("foo") : "foo"
>g : (x: "foo") => "foo"
>"foo" : "foo"
let h = bar(x => x);
>h : (x: "foo" | "bar") => "foo" | "bar"
>bar(x => x) : (x: "foo" | "bar") => "foo" | "bar"
>bar : <T extends "foo" | "bar">(f: (x: T) => T) => (x: T) => T
>x => x : (x: "foo" | "bar") => "foo" | "bar"
>x : "foo" | "bar"
>x : "foo" | "bar"
let hResult = h("foo");
>hResult : "foo" | "bar"
>h("foo") : "foo" | "bar"
>h : (x: "foo" | "bar") => "foo" | "bar"
>"foo" : "foo"
hResult = h("bar");
>hResult = h("bar") : "foo" | "bar"
>hResult : "foo" | "bar"
>h("bar") : "foo" | "bar"
>h : (x: "foo" | "bar") => "foo" | "bar"
>"bar" : "bar"
@@ -0,0 +1,15 @@
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint02.ts(6,13): error TS2345: Argument of type '(y: "foo" | "bar") => string' is not assignable to parameter of type '(x: "foo") => "foo"'.
Type 'string' is not assignable to type '"foo"'.
==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTypeParameterConstraint02.ts (1 errors) ====
function foo<T extends "foo">(f: (x: T) => T) {
return f;
}
let f = foo((y: "foo" | "bar") => y === "foo" ? y : "foo");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(y: "foo" | "bar") => string' is not assignable to parameter of type '(x: "foo") => "foo"'.
!!! error TS2345: Type 'string' is not assignable to type '"foo"'.
let fResult = f("foo");
@@ -0,0 +1,21 @@
//// [stringLiteralTypesAsTypeParameterConstraint02.ts]
function foo<T extends "foo">(f: (x: T) => T) {
return f;
}
let f = foo((y: "foo" | "bar") => y === "foo" ? y : "foo");
let fResult = f("foo");
//// [stringLiteralTypesAsTypeParameterConstraint02.js]
function foo(f) {
return f;
}
var f = foo(function (y) { return y === "foo" ? y : "foo"; });
var fResult = f("foo");
//// [stringLiteralTypesAsTypeParameterConstraint02.d.ts]
declare function foo<T extends "foo">(f: (x: T) => T): (x: T) => T;
declare let f: any;
declare let fResult: any;
@@ -0,0 +1,18 @@
//// [stringLiteralTypesOverloads04.ts]
declare function f(x: (p: "foo" | "bar") => "foo");
f(y => {
let z = y = "foo";
return z;
})
//// [stringLiteralTypesOverloads04.js]
f(function (y) {
var z = y = "foo";
return z;
});
//// [stringLiteralTypesOverloads04.d.ts]
declare function f(x: (p: "foo" | "bar") => "foo"): any;
@@ -0,0 +1,19 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads04.ts ===
declare function f(x: (p: "foo" | "bar") => "foo");
>f : Symbol(f, Decl(stringLiteralTypesOverloads04.ts, 0, 0))
>x : Symbol(x, Decl(stringLiteralTypesOverloads04.ts, 1, 19))
>p : Symbol(p, Decl(stringLiteralTypesOverloads04.ts, 1, 23))
f(y => {
>f : Symbol(f, Decl(stringLiteralTypesOverloads04.ts, 0, 0))
>y : Symbol(y, Decl(stringLiteralTypesOverloads04.ts, 3, 2))
let z = y = "foo";
>z : Symbol(z, Decl(stringLiteralTypesOverloads04.ts, 4, 7))
>y : Symbol(y, Decl(stringLiteralTypesOverloads04.ts, 3, 2))
return z;
>z : Symbol(z, Decl(stringLiteralTypesOverloads04.ts, 4, 7))
})
@@ -0,0 +1,23 @@
=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads04.ts ===
declare function f(x: (p: "foo" | "bar") => "foo");
>f : (x: (p: "foo" | "bar") => "foo") => any
>x : (p: "foo" | "bar") => "foo"
>p : "foo" | "bar"
f(y => {
>f(y => { let z = y = "foo"; return z;}) : any
>f : (x: (p: "foo" | "bar") => "foo") => any
>y => { let z = y = "foo"; return z;} : (y: "foo" | "bar") => "foo"
>y : "foo" | "bar"
let z = y = "foo";
>z : "foo"
>y = "foo" : "foo"
>y : "foo" | "bar"
>"foo" : "foo"
return z;
>z : "foo"
})
+1 -1
View File
@@ -21,5 +21,5 @@ if (typeof x === "object") {
}
else {
x;
>x : symbol | Foo
>x : symbol
}
@@ -0,0 +1,41 @@
//// [typeGuardEnums.ts]
enum E {}
enum V {}
let x: number|string|E|V;
if (typeof x === "number") {
x; // number|E|V
}
else {
x; // string
}
if (typeof x !== "number") {
x; // string
}
else {
x; // number|E|V
}
//// [typeGuardEnums.js]
var E;
(function (E) {
})(E || (E = {}));
var V;
(function (V) {
})(V || (V = {}));
var x;
if (typeof x === "number") {
x; // number|E|V
}
else {
x; // string
}
if (typeof x !== "number") {
x; // string
}
else {
x; // number|E|V
}
@@ -0,0 +1,34 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts ===
enum E {}
>E : Symbol(E, Decl(typeGuardEnums.ts, 0, 0))
enum V {}
>V : Symbol(V, Decl(typeGuardEnums.ts, 0, 9))
let x: number|string|E|V;
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
>E : Symbol(E, Decl(typeGuardEnums.ts, 0, 0))
>V : Symbol(V, Decl(typeGuardEnums.ts, 0, 9))
if (typeof x === "number") {
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
x; // number|E|V
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
}
else {
x; // string
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
}
if (typeof x !== "number") {
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
x; // string
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
}
else {
x; // number|E|V
>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3))
}
@@ -0,0 +1,40 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts ===
enum E {}
>E : E
enum V {}
>V : V
let x: number|string|E|V;
>x : number | string | E | V
>E : E
>V : V
if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : string
>x : number | string | E | V
>"number" : string
x; // number|E|V
>x : number | E | V
}
else {
x; // string
>x : string
}
if (typeof x !== "number") {
>typeof x !== "number" : boolean
>typeof x : string
>x : number | string | E | V
>"number" : string
x; // string
>x : string
}
else {
x; // number|E|V
>x : number | E | V
}
@@ -0,0 +1,31 @@
//// [typeGuardNesting.ts]
let strOrBool: string|boolean;
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
//// [typeGuardNesting.js]
var strOrBool;
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
var label = (typeof strOrBool === 'string') ? strOrBool : "string";
var bool = (typeof strOrBool === 'boolean') ? strOrBool : false;
var label2 = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
var bool2 = (typeof strOrBool !== 'string') ? strOrBool : false;
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
var label = (typeof strOrBool === 'string') ? strOrBool : "string";
var bool = (typeof strOrBool === 'boolean') ? strOrBool : false;
var label2 = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
var bool2 = (typeof strOrBool !== 'string') ? strOrBool : false;
}
@@ -0,0 +1,56 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts ===
let strOrBool: string|boolean;
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
>label : Symbol(label, Decl(typeGuardNesting.ts, 2, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
>bool : Symbol(bool, Decl(typeGuardNesting.ts, 3, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
>label2 : Symbol(label2, Decl(typeGuardNesting.ts, 4, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
>bool2 : Symbol(bool2, Decl(typeGuardNesting.ts, 5, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
>label : Symbol(label, Decl(typeGuardNesting.ts, 9, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
>bool : Symbol(bool, Decl(typeGuardNesting.ts, 10, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
>label2 : Symbol(label2, Decl(typeGuardNesting.ts, 11, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
>bool2 : Symbol(bool2, Decl(typeGuardNesting.ts, 12, 4))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3))
}
@@ -0,0 +1,124 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts ===
let strOrBool: string|boolean;
>strOrBool : string | boolean
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
>(typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string' : boolean
>(typeof strOrBool === 'boolean' && !strOrBool) : boolean
>typeof strOrBool === 'boolean' && !strOrBool : boolean
>typeof strOrBool === 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : string | boolean
>'boolean' : string
>!strOrBool : boolean
>strOrBool : boolean
>typeof strOrBool === 'string' : boolean
>typeof strOrBool : string
>strOrBool : string | boolean
>'string' : string
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
>label : string
>(typeof strOrBool === 'string') ? strOrBool : "string" : string
>(typeof strOrBool === 'string') : boolean
>typeof strOrBool === 'string' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'string' : string
>strOrBool : string
>"string" : string
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
>bool : boolean
>(typeof strOrBool === 'boolean') ? strOrBool : false : boolean
>(typeof strOrBool === 'boolean') : boolean
>typeof strOrBool === 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'boolean' : string
>strOrBool : boolean
>false : boolean
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
>label2 : string
>(typeof strOrBool !== 'boolean') ? strOrBool : "string" : string
>(typeof strOrBool !== 'boolean') : boolean
>typeof strOrBool !== 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'boolean' : string
>strOrBool : string
>"string" : string
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
>bool2 : boolean
>(typeof strOrBool !== 'string') ? strOrBool : false : boolean
>(typeof strOrBool !== 'string') : boolean
>typeof strOrBool !== 'string' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'string' : string
>strOrBool : boolean
>false : boolean
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
>(typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean' : boolean
>(typeof strOrBool !== 'string' && !strOrBool) : boolean
>typeof strOrBool !== 'string' && !strOrBool : boolean
>typeof strOrBool !== 'string' : boolean
>typeof strOrBool : string
>strOrBool : string | boolean
>'string' : string
>!strOrBool : boolean
>strOrBool : boolean
>typeof strOrBool !== 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : string | boolean
>'boolean' : string
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
>label : string
>(typeof strOrBool === 'string') ? strOrBool : "string" : string
>(typeof strOrBool === 'string') : boolean
>typeof strOrBool === 'string' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'string' : string
>strOrBool : string
>"string" : string
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
>bool : boolean
>(typeof strOrBool === 'boolean') ? strOrBool : false : boolean
>(typeof strOrBool === 'boolean') : boolean
>typeof strOrBool === 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'boolean' : string
>strOrBool : boolean
>false : boolean
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
>label2 : string
>(typeof strOrBool !== 'boolean') ? strOrBool : "string" : string
>(typeof strOrBool !== 'boolean') : boolean
>typeof strOrBool !== 'boolean' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'boolean' : string
>strOrBool : string
>"string" : string
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
>bool2 : boolean
>(typeof strOrBool !== 'string') ? strOrBool : false : boolean
>(typeof strOrBool !== 'string') : boolean
>typeof strOrBool !== 'string' : boolean
>typeof strOrBool : string
>strOrBool : boolean | string
>'string' : string
>strOrBool : boolean
>false : boolean
}
@@ -23,19 +23,19 @@ if (typeof strOrC === "Object") {
c = strOrC; // C
}
else {
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
}
if (typeof numOrC === "Object") {
c = numOrC; // C
}
else {
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
}
if (typeof boolOrC === "Object") {
c = boolOrC; // C
}
else {
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
}
// Narrowing occurs only if target type is a subtype of variable type
@@ -50,19 +50,19 @@ else {
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrC !== "Object") {
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
}
else {
c = strOrC; // C
}
if (typeof numOrC !== "Object") {
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
}
else {
c = numOrC; // C
}
if (typeof boolOrC !== "Object") {
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
}
else {
c = boolOrC; // C
@@ -104,19 +104,19 @@ if (typeof strOrC === "Object") {
c = strOrC; // C
}
else {
var r2 = strOrC; // string | C
var r2 = strOrC; // string
}
if (typeof numOrC === "Object") {
c = numOrC; // C
}
else {
var r3 = numOrC; // number | C
var r3 = numOrC; // number
}
if (typeof boolOrC === "Object") {
c = boolOrC; // C
}
else {
var r4 = boolOrC; // boolean | C
var r4 = boolOrC; // boolean
}
// Narrowing occurs only if target type is a subtype of variable type
if (typeof strOrNumOrBool === "Object") {
@@ -129,19 +129,19 @@ else {
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrC !== "Object") {
var r2 = strOrC; // string | C
var r2 = strOrC; // string
}
else {
c = strOrC; // C
}
if (typeof numOrC !== "Object") {
var r3 = numOrC; // number | C
var r3 = numOrC; // number
}
else {
c = numOrC; // C
}
if (typeof boolOrC !== "Object") {
var r4 = boolOrC; // boolean | C
var r4 = boolOrC; // boolean
}
else {
c = boolOrC; // C
@@ -56,9 +56,8 @@ if (typeof strOrC === "Object") {
>strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3))
}
else {
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
>r2 : Symbol(r2, Decl(typeGuardOfFormTypeOfOther.ts, 24, 7), Decl(typeGuardOfFormTypeOfOther.ts, 51, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3))
}
if (typeof numOrC === "Object") {
@@ -69,9 +68,8 @@ if (typeof numOrC === "Object") {
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3))
}
else {
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
>r3 : Symbol(r3, Decl(typeGuardOfFormTypeOfOther.ts, 30, 7), Decl(typeGuardOfFormTypeOfOther.ts, 57, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3))
}
if (typeof boolOrC === "Object") {
@@ -82,9 +80,8 @@ if (typeof boolOrC === "Object") {
>boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3))
}
else {
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
>r4 : Symbol(r4, Decl(typeGuardOfFormTypeOfOther.ts, 36, 7), Decl(typeGuardOfFormTypeOfOther.ts, 63, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3))
}
@@ -108,9 +105,8 @@ else {
if (typeof strOrC !== "Object") {
>strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3))
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
>r2 : Symbol(r2, Decl(typeGuardOfFormTypeOfOther.ts, 24, 7), Decl(typeGuardOfFormTypeOfOther.ts, 51, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3))
}
else {
@@ -121,9 +117,8 @@ else {
if (typeof numOrC !== "Object") {
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3))
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
>r3 : Symbol(r3, Decl(typeGuardOfFormTypeOfOther.ts, 30, 7), Decl(typeGuardOfFormTypeOfOther.ts, 57, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3))
}
else {
@@ -134,9 +129,8 @@ else {
if (typeof boolOrC !== "Object") {
>boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3))
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
>r4 : Symbol(r4, Decl(typeGuardOfFormTypeOfOther.ts, 36, 7), Decl(typeGuardOfFormTypeOfOther.ts, 63, 7))
>C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0))
>boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3))
}
else {
@@ -60,10 +60,9 @@ if (typeof strOrC === "Object") {
>strOrC : C
}
else {
var r2: string | C = strOrC; // string | C
>r2 : string | C
>C : C
>strOrC : string | C
var r2: string = strOrC; // string
>r2 : string
>strOrC : string
}
if (typeof numOrC === "Object") {
>typeof numOrC === "Object" : boolean
@@ -77,10 +76,9 @@ if (typeof numOrC === "Object") {
>numOrC : C
}
else {
var r3: number | C = numOrC; // number | C
>r3 : number | C
>C : C
>numOrC : number | C
var r3: number = numOrC; // number
>r3 : number
>numOrC : number
}
if (typeof boolOrC === "Object") {
>typeof boolOrC === "Object" : boolean
@@ -94,10 +92,9 @@ if (typeof boolOrC === "Object") {
>boolOrC : C
}
else {
var r4: boolean | C = boolOrC; // boolean | C
>r4 : boolean | C
>C : C
>boolOrC : boolean | C
var r4: boolean = boolOrC; // boolean
>r4 : boolean
>boolOrC : boolean
}
// Narrowing occurs only if target type is a subtype of variable type
@@ -126,10 +123,9 @@ if (typeof strOrC !== "Object") {
>strOrC : string | C
>"Object" : string
var r2: string | C = strOrC; // string | C
>r2 : string | C
>C : C
>strOrC : string | C
var r2: string = strOrC; // string
>r2 : string
>strOrC : string
}
else {
c = strOrC; // C
@@ -143,10 +139,9 @@ if (typeof numOrC !== "Object") {
>numOrC : number | C
>"Object" : string
var r3: number | C = numOrC; // number | C
>r3 : number | C
>C : C
>numOrC : number | C
var r3: number = numOrC; // number
>r3 : number
>numOrC : number
}
else {
c = numOrC; // C
@@ -160,10 +155,9 @@ if (typeof boolOrC !== "Object") {
>boolOrC : boolean | C
>"Object" : string
var r4: boolean | C = boolOrC; // boolean | C
>r4 : boolean | C
>C : C
>boolOrC : boolean | C
var r4: boolean = boolOrC; // boolean
>r4 : boolean
>boolOrC : boolean
}
else {
c = boolOrC; // C
@@ -0,0 +1,17 @@
//// [typeGuardRedundancy.ts]
var x: string|number;
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
//// [typeGuardRedundancy.js]
var x;
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
@@ -0,0 +1,48 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts ===
var x: string|number;
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
>r1 : Symbol(r1, Decl(typeGuardRedundancy.ts, 2, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
>r2 : Symbol(r2, Decl(typeGuardRedundancy.ts, 4, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
>r3 : Symbol(r3, Decl(typeGuardRedundancy.ts, 6, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
>r4 : Symbol(r4, Decl(typeGuardRedundancy.ts, 8, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --))
>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3))
>substr : Symbol(String.substr, Decl(lib.d.ts, --, --))
@@ -0,0 +1,84 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts ===
var x: string|number;
>x : string | number
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
>r1 : (from: number, length?: number) => string
>typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string
>typeof x === "string" && typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : string
>"string" : string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
>r2 : (fractionDigits?: number) => string
>!(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string
>!(typeof x === "string" && typeof x === "string") : boolean
>(typeof x === "string" && typeof x === "string") : boolean
>typeof x === "string" && typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : string
>"string" : string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
>r3 : (from: number, length?: number) => string
>typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string
>typeof x === "string" || typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : number
>"string" : string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
>r4 : (fractionDigits?: number) => string
>!(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string
>!(typeof x === "string" || typeof x === "string") : boolean
>(typeof x === "string" || typeof x === "string") : boolean
>typeof x === "string" || typeof x === "string" : boolean
>typeof x === "string" : boolean
>typeof x : string
>x : string | number
>"string" : string
>typeof x === "string" : boolean
>typeof x : string
>x : number
>"string" : string
>x.toFixed : (fractionDigits?: number) => string
>x : number
>toFixed : (fractionDigits?: number) => string
>x.substr : (from: number, length?: number) => string
>x : string
>substr : (from: number, length?: number) => string
@@ -0,0 +1,24 @@
//// [typeGuardTautologicalConsistiency.ts]
let stringOrNumber: string | number;
if (typeof stringOrNumber === "number") {
if (typeof stringOrNumber !== "number") {
stringOrNumber;
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
stringOrNumber;
}
//// [typeGuardTautologicalConsistiency.js]
var stringOrNumber;
if (typeof stringOrNumber === "number") {
if (typeof stringOrNumber !== "number") {
stringOrNumber;
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
stringOrNumber;
}
@@ -0,0 +1,23 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts ===
let stringOrNumber: string | number;
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
if (typeof stringOrNumber === "number") {
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
if (typeof stringOrNumber !== "number") {
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
stringOrNumber;
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
stringOrNumber;
>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3))
}
@@ -0,0 +1,36 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts ===
let stringOrNumber: string | number;
>stringOrNumber : string | number
if (typeof stringOrNumber === "number") {
>typeof stringOrNumber === "number" : boolean
>typeof stringOrNumber : string
>stringOrNumber : string | number
>"number" : string
if (typeof stringOrNumber !== "number") {
>typeof stringOrNumber !== "number" : boolean
>typeof stringOrNumber : string
>stringOrNumber : number
>"number" : string
stringOrNumber;
>stringOrNumber : string | number
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
>typeof stringOrNumber === "number" && typeof stringOrNumber !== "number" : boolean
>typeof stringOrNumber === "number" : boolean
>typeof stringOrNumber : string
>stringOrNumber : string | number
>"number" : string
>typeof stringOrNumber !== "number" : boolean
>typeof stringOrNumber : string
>stringOrNumber : number
>"number" : string
stringOrNumber;
>stringOrNumber : string | number
}
@@ -0,0 +1,15 @@
// @module: commonjs
// @target: ES5
// @filename: m1.ts
export default function f() {
}
export {f};
// @filename: m2.ts
import foo from "./m1";
import {f} from "./m1";
f();
foo();
@@ -0,0 +1,18 @@
enum E {}
enum V {}
let x: number|string|E|V;
if (typeof x === "number") {
x; // number|E|V
}
else {
x; // string
}
if (typeof x !== "number") {
x; // string
}
else {
x; // number|E|V
}
@@ -0,0 +1,14 @@
let strOrBool: string|boolean;
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
@@ -22,19 +22,19 @@ if (typeof strOrC === "Object") {
c = strOrC; // C
}
else {
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
}
if (typeof numOrC === "Object") {
c = numOrC; // C
}
else {
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
}
if (typeof boolOrC === "Object") {
c = boolOrC; // C
}
else {
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
}
// Narrowing occurs only if target type is a subtype of variable type
@@ -49,19 +49,19 @@ else {
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrC !== "Object") {
var r2: string | C = strOrC; // string | C
var r2: string = strOrC; // string
}
else {
c = strOrC; // C
}
if (typeof numOrC !== "Object") {
var r3: number | C = numOrC; // number | C
var r3: number = numOrC; // number
}
else {
c = numOrC; // C
}
if (typeof boolOrC !== "Object") {
var r4: boolean | C = boolOrC; // boolean | C
var r4: boolean = boolOrC; // boolean
}
else {
c = boolOrC; // C
@@ -0,0 +1,9 @@
var x: string|number;
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr;
@@ -0,0 +1,11 @@
let stringOrNumber: string | number;
if (typeof stringOrNumber === "number") {
if (typeof stringOrNumber !== "number") {
stringOrNumber;
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
stringOrNumber;
}
@@ -0,0 +1,9 @@
// @declaration: true
declare function myRandBool(): boolean;
let a: "foo" = "foo";
let b = a || "foo";
let c: "foo" = b;
let d = b || "bar";
let e: "foo" | "bar" = d;
@@ -0,0 +1,8 @@
// @declaration: true
declare function myRandBool(): boolean;
let a: "foo" = ("foo");
let b: "foo" | "bar" = ("foo");
let c: "foo" = (myRandBool ? "foo" : ("foo"));
let d: "foo" | "bar" = (myRandBool ? "foo" : ("bar"));
@@ -0,0 +1,19 @@
// @declaration: true
function foo<T extends "foo">(f: (x: T) => T) {
return f;
}
function bar<T extends "foo" | "bar">(f: (x: T) => T) {
return f;
}
let f = foo(x => x);
let fResult = f("foo");
let g = foo((x => x));
let gResult = g("foo");
let h = bar(x => x);
let hResult = h("foo");
hResult = h("bar");
@@ -0,0 +1,8 @@
// @declaration: true
function foo<T extends "foo">(f: (x: T) => T) {
return f;
}
let f = foo((y: "foo" | "bar") => y === "foo" ? y : "foo");
let fResult = f("foo");
@@ -0,0 +1,8 @@
// @declaration: true
declare function f(x: (p: "foo" | "bar") => "foo");
f(y => {
let z = y = "foo";
return z;
})
@@ -0,0 +1,28 @@
/// <reference path="./fourslash.ts" />
////interface IFoo {
//// [|a|]: string;
////}
////class C<T extends IFoo> {
//// method() {
//// var x: T = {
//// [|a|]: ""
//// };
//// x.[|a|];
//// }
////}
////
////
////var x: IFoo = {
//// [|a|]: "ss"
////};
let ranges = test.ranges()
for (let range of ranges) {
goTo.position(range.start);
verify.referencesCountIs(ranges.length);
for (let expectedReference of ranges) {
verify.referencesAtPositionContains(expectedReference);
}
}