diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b43c86b0f6d2e..8a3f229719d46 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -857,10 +857,11 @@ module ts { return symbol; } } + let fileName: string; let sourceFile: SourceFile; while (true) { - let fileName = normalizePath(combinePaths(searchPath, moduleName)); - sourceFile = host.getSourceFile(fileName + ".ts") || host.getSourceFile(fileName + ".d.ts"); + fileName = normalizePath(combinePaths(searchPath, moduleName)); + sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension)); if (sourceFile || isRelative) { break; } @@ -5398,38 +5399,43 @@ module ts { if (!isTypeSubtypeOf(rightType, globalFunctionType)) { return type; } - // Target type is type of prototype property + + let targetType: Type; let prototypeProperty = getPropertyOfType(rightType, "prototype"); if (prototypeProperty) { - let targetType = getTypeOfSymbol(prototypeProperty); - if (targetType !== anyType) { - // Narrow to the target type if it's a subtype of the current type - if (isTypeSubtypeOf(targetType, type)) { - return targetType; - } - // If the current type is a union type, remove all constituents that aren't subtypes of the target. - if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, targetType))); - } + // Target type is type of the protoype property + let prototypePropertyType = getTypeOfSymbol(prototypeProperty); + if (prototypePropertyType !== anyType) { + targetType = prototypePropertyType; } } - // Target type is type of construct signature - let constructSignatures: Signature[]; - if (rightType.flags & TypeFlags.Interface) { - constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; - } - else if (rightType.flags & TypeFlags.Anonymous) { - constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct); + + if (!targetType) { + // Target type is type of construct signature + let constructSignatures: Signature[]; + if (rightType.flags & TypeFlags.Interface) { + constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; + } + else if (rightType.flags & TypeFlags.Anonymous) { + constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct); + } + + if (constructSignatures && constructSignatures.length) { + targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature)))); + } } - if (constructSignatures && constructSignatures.length !== 0) { - let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature)))); - // Pickup type from union types + if (targetType) { + // Narrow to the target type if it's a subtype of the current type + if (isTypeSubtypeOf(targetType, type)) { + return targetType; + } + // If the current type is a union type, remove all constituents that aren't subtypes of the target. if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, instanceType))); + return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, targetType))); } - return instanceType; } + return type; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 9b987ba77c619..9fe9747fb57b3 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -640,16 +640,18 @@ module ts { return pathLen > extLen && path.substr(pathLen - extLen, extLen) === extension; } - let supportedExtensions = [".d.ts", ".ts", ".js"]; + /** + * List of supported extensions in order of file resolution precedence. + */ + export const supportedExtensions = [".ts", ".d.ts"]; + const extensionsToRemove = [".d.ts", ".ts", ".js"]; export function removeFileExtension(path: string): string { - for (let ext of supportedExtensions) { - + for (let ext of extensionsToRemove) { if (fileExtensionIs(path, ext)) { return path.substr(0, path.length - ext.length); } } - return path; } diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 1f8edf8fee367..52718c6ddc403 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -498,7 +498,7 @@ module ts { Corrupted_locale_file_0: { code: 6051, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." }, Raise_error_on_expressions_and_declarations_with_an_implied_any_type: { code: 6052, category: DiagnosticCategory.Message, key: "Raise error on expressions and declarations with an implied 'any' type." }, File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." }, - File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." }, + File_0_has_unsupported_extension_The_only_supported_extensions_are_1: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' has unsupported extension. The only supported extensions are {1}." }, Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." }, Do_not_emit_declarations_for_code_that_has_an_internal_annotation: { code: 6056, category: DiagnosticCategory.Message, key: "Do not emit declarations for code that has an '@internal' annotation." }, Preserve_new_lines_when_emitting_code: { code: 6057, category: DiagnosticCategory.Message, key: "Preserve new-lines when emitting code." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 42eae83e26946..d7446907d232c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1982,7 +1982,7 @@ "category": "Error", "code": 6053 }, - "File '{0}' must have extension '.ts' or '.d.ts'.": { + "File '{0}' has unsupported extension. The only supported extensions are {1}.": { "category": "Error", "code": 6054 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 1e628326ba7e2..0eb6f62f9eee4 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -307,38 +307,45 @@ module ts { function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) { let start: number; let length: number; + let extensions: string; + let diagnosticArgument: string[]; if (refEnd !== undefined && refPos !== undefined) { start = refPos; length = refEnd - refPos; } let diagnostic: DiagnosticMessage; if (hasExtension(fileName)) { - if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) { - diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts; + if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) { + diagnostic = Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1; + diagnosticArgument = [fileName, "'" + supportedExtensions.join("', '") + "'"]; } else if (!findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) { diagnostic = Diagnostics.File_0_not_found; + diagnosticArgument = [fileName]; } else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) { diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself; + diagnosticArgument = [fileName]; } } else { if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) { diagnostic = Diagnostics.File_0_not_found; + diagnosticArgument = [fileName]; } - else if (!findSourceFile(fileName + ".ts", isDefaultLib, refFile, refPos, refEnd) && !findSourceFile(fileName + ".d.ts", isDefaultLib, refFile, refPos, refEnd)) { + else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) { diagnostic = Diagnostics.File_0_not_found; fileName += ".ts"; + diagnosticArgument = [fileName]; } } if (diagnostic) { if (refFile) { - diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, fileName)); + diagnostics.add(createFileDiagnostic(refFile, start, length, diagnostic, ...diagnosticArgument)); } else { - diagnostics.add(createCompilerDiagnostic(diagnostic, fileName)); + diagnostics.add(createCompilerDiagnostic(diagnostic, ...diagnosticArgument)); } } } @@ -417,9 +424,10 @@ module ts { let moduleNameText = (moduleNameExpr).text; if (moduleNameText) { let searchPath = basePath; + let searchName: string; while (true) { - let searchName = normalizePath(combinePaths(searchPath, moduleNameText)); - if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) { + searchName = normalizePath(combinePaths(searchPath, moduleNameText)); + if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) { break; } let parentPath = getDirectoryPath(searchPath); @@ -448,10 +456,7 @@ module ts { // An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules // only through top - level external module names. Relative external module names are not permitted. let searchName = normalizePath(combinePaths(basePath, moduleName)); - let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral); - if (!tsFile) { - findModuleSourceFile(searchName + ".d.ts", nameLiteral); - } + forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, nameLiteral)); } } }); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0bb0065d1b0a9..f63007bddba38 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -484,9 +484,6 @@ module ts { case SyntaxKind.IndexSignature: case SyntaxKind.FunctionType: case SyntaxKind.ConstructorType: - case SyntaxKind.FunctionExpression: - case SyntaxKind.ArrowFunction: - case SyntaxKind.FunctionDeclaration: return true; } } diff --git a/src/lib/es6.d.ts b/src/lib/es6.d.ts index fb0c64f849522..dc082c8f0a62e 100644 --- a/src/lib/es6.d.ts +++ b/src/lib/es6.d.ts @@ -3580,6 +3580,7 @@ interface PromiseLike { * @returns A Promise for the completion of which ever callback is executed. */ then(onfulfilled?: (value: T) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): PromiseLike; + then(onfulfilled?: (value: T) => TResult | PromiseLike, onrejected?: (reason: any) => void): PromiseLike; } /** @@ -3593,6 +3594,7 @@ interface Promise { * @returns A Promise for the completion of which ever callback is executed. */ then(onfulfilled?: (value: T) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): Promise; + then(onfulfilled?: (value: T) => TResult | PromiseLike, onrejected?: (reason: any) => void): Promise; /** * Attaches a callback for only the rejection of the Promise. diff --git a/tests/baselines/reference/narrowTypeByInstanceof.js b/tests/baselines/reference/narrowTypeByInstanceof.js new file mode 100644 index 0000000000000..11f55089bdac9 --- /dev/null +++ b/tests/baselines/reference/narrowTypeByInstanceof.js @@ -0,0 +1,53 @@ +//// [narrowTypeByInstanceof.ts] + class Match { + public range(): any { + return undefined; + } + } + + class FileMatch { + public resource(): any { + return undefined; + } + } + +type FileMatchOrMatch = FileMatch | Match; + + +let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch; + +if (elementA instanceof FileMatch && elementB instanceof FileMatch) { + let a = elementA.resource().path; + let b = elementB.resource().path; +} else if (elementA instanceof Match && elementB instanceof Match) { + let a = elementA.range(); + let b = elementB.range(); +} + + +//// [narrowTypeByInstanceof.js] +var Match = (function () { + function Match() { + } + Match.prototype.range = function () { + return undefined; + }; + return Match; +})(); +var FileMatch = (function () { + function FileMatch() { + } + FileMatch.prototype.resource = function () { + return undefined; + }; + return FileMatch; +})(); +var elementA, elementB; +if (elementA instanceof FileMatch && elementB instanceof FileMatch) { + var a = elementA.resource().path; + var b = elementB.resource().path; +} +else if (elementA instanceof Match && elementB instanceof Match) { + var a = elementA.range(); + var b = elementB.range(); +} diff --git a/tests/baselines/reference/narrowTypeByInstanceof.symbols b/tests/baselines/reference/narrowTypeByInstanceof.symbols new file mode 100644 index 0000000000000..3e620fd105ca1 --- /dev/null +++ b/tests/baselines/reference/narrowTypeByInstanceof.symbols @@ -0,0 +1,72 @@ +=== tests/cases/compiler/narrowTypeByInstanceof.ts === + class Match { +>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0)) + + public range(): any { +>range : Symbol(range, Decl(narrowTypeByInstanceof.ts, 0, 17)) + + return undefined; +>undefined : Symbol(undefined) + } + } + + class FileMatch { +>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5)) + + public resource(): any { +>resource : Symbol(resource, Decl(narrowTypeByInstanceof.ts, 6, 21)) + + return undefined; +>undefined : Symbol(undefined) + } + } + +type FileMatchOrMatch = FileMatch | Match; +>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5)) +>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5)) +>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0)) + + +let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch; +>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3)) +>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5)) +>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31)) +>FileMatchOrMatch : Symbol(FileMatchOrMatch, Decl(narrowTypeByInstanceof.ts, 10, 5)) + +if (elementA instanceof FileMatch && elementB instanceof FileMatch) { +>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3)) +>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5)) +>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31)) +>FileMatch : Symbol(FileMatch, Decl(narrowTypeByInstanceof.ts, 4, 5)) + + let a = elementA.resource().path; +>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 18, 7)) +>elementA.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21)) +>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3)) +>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21)) + + let b = elementB.resource().path; +>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 19, 7)) +>elementB.resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21)) +>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31)) +>resource : Symbol(FileMatch.resource, Decl(narrowTypeByInstanceof.ts, 6, 21)) + +} else if (elementA instanceof Match && elementB instanceof Match) { +>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3)) +>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0)) +>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31)) +>Match : Symbol(Match, Decl(narrowTypeByInstanceof.ts, 0, 0)) + + let a = elementA.range(); +>a : Symbol(a, Decl(narrowTypeByInstanceof.ts, 21, 7)) +>elementA.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17)) +>elementA : Symbol(elementA, Decl(narrowTypeByInstanceof.ts, 15, 3)) +>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17)) + + let b = elementB.range(); +>b : Symbol(b, Decl(narrowTypeByInstanceof.ts, 22, 7)) +>elementB.range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17)) +>elementB : Symbol(elementB, Decl(narrowTypeByInstanceof.ts, 15, 31)) +>range : Symbol(Match.range, Decl(narrowTypeByInstanceof.ts, 0, 17)) +} + diff --git a/tests/baselines/reference/narrowTypeByInstanceof.types b/tests/baselines/reference/narrowTypeByInstanceof.types new file mode 100644 index 0000000000000..b98b1e4d234e0 --- /dev/null +++ b/tests/baselines/reference/narrowTypeByInstanceof.types @@ -0,0 +1,86 @@ +=== tests/cases/compiler/narrowTypeByInstanceof.ts === + class Match { +>Match : Match + + public range(): any { +>range : () => any + + return undefined; +>undefined : undefined + } + } + + class FileMatch { +>FileMatch : FileMatch + + public resource(): any { +>resource : () => any + + return undefined; +>undefined : undefined + } + } + +type FileMatchOrMatch = FileMatch | Match; +>FileMatchOrMatch : Match | FileMatch +>FileMatch : FileMatch +>Match : Match + + +let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch; +>elementA : Match | FileMatch +>FileMatchOrMatch : Match | FileMatch +>elementB : Match | FileMatch +>FileMatchOrMatch : Match | FileMatch + +if (elementA instanceof FileMatch && elementB instanceof FileMatch) { +>elementA instanceof FileMatch && elementB instanceof FileMatch : boolean +>elementA instanceof FileMatch : boolean +>elementA : Match | FileMatch +>FileMatch : typeof FileMatch +>elementB instanceof FileMatch : boolean +>elementB : Match | FileMatch +>FileMatch : typeof FileMatch + + let a = elementA.resource().path; +>a : any +>elementA.resource().path : any +>elementA.resource() : any +>elementA.resource : () => any +>elementA : FileMatch +>resource : () => any +>path : any + + let b = elementB.resource().path; +>b : any +>elementB.resource().path : any +>elementB.resource() : any +>elementB.resource : () => any +>elementB : FileMatch +>resource : () => any +>path : any + +} else if (elementA instanceof Match && elementB instanceof Match) { +>elementA instanceof Match && elementB instanceof Match : boolean +>elementA instanceof Match : boolean +>elementA : Match | FileMatch +>Match : typeof Match +>elementB instanceof Match : boolean +>elementB : Match | FileMatch +>Match : typeof Match + + let a = elementA.range(); +>a : any +>elementA.range() : any +>elementA.range : () => any +>elementA : Match +>range : () => any + + let b = elementB.range(); +>b : any +>elementB.range() : any +>elementB.range : () => any +>elementB : Match +>range : () => any +} + diff --git a/tests/baselines/reference/project/invalidRootFile/amd/invalidRootFile.errors.txt b/tests/baselines/reference/project/invalidRootFile/amd/invalidRootFile.errors.txt index 25059478bf5a6..1bda32a0b4cfc 100644 --- a/tests/baselines/reference/project/invalidRootFile/amd/invalidRootFile.errors.txt +++ b/tests/baselines/reference/project/invalidRootFile/amd/invalidRootFile.errors.txt @@ -1,6 +1,6 @@ -error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'. +error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'. error TS6053: File 'a.ts' not found. -!!! error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'. +!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'. !!! error TS6053: File 'a.ts' not found. \ No newline at end of file diff --git a/tests/baselines/reference/project/invalidRootFile/node/invalidRootFile.errors.txt b/tests/baselines/reference/project/invalidRootFile/node/invalidRootFile.errors.txt index 25059478bf5a6..1bda32a0b4cfc 100644 --- a/tests/baselines/reference/project/invalidRootFile/node/invalidRootFile.errors.txt +++ b/tests/baselines/reference/project/invalidRootFile/node/invalidRootFile.errors.txt @@ -1,6 +1,6 @@ -error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'. +error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'. error TS6053: File 'a.ts' not found. -!!! error TS6054: File 'a.t' must have extension '.ts' or '.d.ts'. +!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.d.ts'. !!! error TS6053: File 'a.ts' not found. \ No newline at end of file diff --git a/tests/baselines/reference/promiseVoidErrorCallback.js b/tests/baselines/reference/promiseVoidErrorCallback.js new file mode 100644 index 0000000000000..a9aa0f1b5d081 --- /dev/null +++ b/tests/baselines/reference/promiseVoidErrorCallback.js @@ -0,0 +1,43 @@ +//// [promiseVoidErrorCallback.ts] +interface T1 { + __t1: string; +} + +interface T2 { + __t2: string; +} + +interface T3 { + __t3: string; +} + +function f1(): Promise { + return Promise.resolve({ __t1: "foo_t1" }); +} + +function f2(x: T1): T2 { + return { __t2: x.__t1 + ":foo_21" }; +} + +var x3 = f1() + .then(f2, (e: Error) => { + throw e; +}) + .then((x: T2) => { + return { __t3: x.__t2 + "bar" }; +}); + +//// [promiseVoidErrorCallback.js] +function f1() { + return Promise.resolve({ __t1: "foo_t1" }); +} +function f2(x) { + return { __t2: x.__t1 + ":foo_21" }; +} +var x3 = f1() + .then(f2, (e) => { + throw e; +}) + .then((x) => { + return { __t3: x.__t2 + "bar" }; +}); diff --git a/tests/baselines/reference/promiseVoidErrorCallback.symbols b/tests/baselines/reference/promiseVoidErrorCallback.symbols new file mode 100644 index 0000000000000..78faacdb1f14a --- /dev/null +++ b/tests/baselines/reference/promiseVoidErrorCallback.symbols @@ -0,0 +1,75 @@ +=== tests/cases/compiler/promiseVoidErrorCallback.ts === +interface T1 { +>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0)) + + __t1: string; +>__t1 : Symbol(__t1, Decl(promiseVoidErrorCallback.ts, 0, 14)) +} + +interface T2 { +>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1)) + + __t2: string; +>__t2 : Symbol(__t2, Decl(promiseVoidErrorCallback.ts, 4, 14)) +} + +interface T3 { +>T3 : Symbol(T3, Decl(promiseVoidErrorCallback.ts, 6, 1)) + + __t3: string; +>__t3 : Symbol(__t3, Decl(promiseVoidErrorCallback.ts, 8, 14)) +} + +function f1(): Promise { +>f1 : Symbol(f1, Decl(promiseVoidErrorCallback.ts, 10, 1)) +>Promise : Symbol(Promise, Decl(lib.d.ts, 4769, 1), Decl(lib.d.ts, 4854, 11)) +>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0)) + + return Promise.resolve({ __t1: "foo_t1" }); +>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.d.ts, 4836, 39), Decl(lib.d.ts, 4843, 54)) +>Promise : Symbol(Promise, Decl(lib.d.ts, 4769, 1), Decl(lib.d.ts, 4854, 11)) +>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.d.ts, 4836, 39), Decl(lib.d.ts, 4843, 54)) +>__t1 : Symbol(__t1, Decl(promiseVoidErrorCallback.ts, 13, 28)) +} + +function f2(x: T1): T2 { +>f2 : Symbol(f2, Decl(promiseVoidErrorCallback.ts, 14, 1)) +>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 16, 12)) +>T1 : Symbol(T1, Decl(promiseVoidErrorCallback.ts, 0, 0)) +>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1)) + + return { __t2: x.__t1 + ":foo_21" }; +>__t2 : Symbol(__t2, Decl(promiseVoidErrorCallback.ts, 17, 12)) +>x.__t1 : Symbol(T1.__t1, Decl(promiseVoidErrorCallback.ts, 0, 14)) +>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 16, 12)) +>__t1 : Symbol(T1.__t1, Decl(promiseVoidErrorCallback.ts, 0, 14)) +} + +var x3 = f1() +>x3 : Symbol(x3, Decl(promiseVoidErrorCallback.ts, 20, 3)) +>f1() .then(f2, (e: Error) => { throw e;}) .then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158)) +>f1() .then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158)) +>f1 : Symbol(f1, Decl(promiseVoidErrorCallback.ts, 10, 1)) + + .then(f2, (e: Error) => { +>then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158)) +>f2 : Symbol(f2, Decl(promiseVoidErrorCallback.ts, 14, 1)) +>e : Symbol(e, Decl(promiseVoidErrorCallback.ts, 21, 15)) +>Error : Symbol(Error, Decl(lib.d.ts, 876, 38), Decl(lib.d.ts, 889, 11)) + + throw e; +>e : Symbol(e, Decl(promiseVoidErrorCallback.ts, 21, 15)) + +}) + .then((x: T2) => { +>then : Symbol(Promise.then, Decl(lib.d.ts, 4774, 22), Decl(lib.d.ts, 4781, 158)) +>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 24, 11)) +>T2 : Symbol(T2, Decl(promiseVoidErrorCallback.ts, 2, 1)) + + return { __t3: x.__t2 + "bar" }; +>__t3 : Symbol(__t3, Decl(promiseVoidErrorCallback.ts, 25, 12)) +>x.__t2 : Symbol(T2.__t2, Decl(promiseVoidErrorCallback.ts, 4, 14)) +>x : Symbol(x, Decl(promiseVoidErrorCallback.ts, 24, 11)) +>__t2 : Symbol(T2.__t2, Decl(promiseVoidErrorCallback.ts, 4, 14)) + +}); diff --git a/tests/baselines/reference/promiseVoidErrorCallback.types b/tests/baselines/reference/promiseVoidErrorCallback.types new file mode 100644 index 0000000000000..82b248f64bdb8 --- /dev/null +++ b/tests/baselines/reference/promiseVoidErrorCallback.types @@ -0,0 +1,89 @@ +=== tests/cases/compiler/promiseVoidErrorCallback.ts === +interface T1 { +>T1 : T1 + + __t1: string; +>__t1 : string +} + +interface T2 { +>T2 : T2 + + __t2: string; +>__t2 : string +} + +interface T3 { +>T3 : T3 + + __t3: string; +>__t3 : string +} + +function f1(): Promise { +>f1 : () => Promise +>Promise : Promise +>T1 : T1 + + return Promise.resolve({ __t1: "foo_t1" }); +>Promise.resolve({ __t1: "foo_t1" }) : Promise<{ __t1: string; }> +>Promise.resolve : { (value: T | PromiseLike): Promise; (): Promise; } +>Promise : PromiseConstructor +>resolve : { (value: T | PromiseLike): Promise; (): Promise; } +>{ __t1: "foo_t1" } : { __t1: string; } +>__t1 : string +>"foo_t1" : string +} + +function f2(x: T1): T2 { +>f2 : (x: T1) => T2 +>x : T1 +>T1 : T1 +>T2 : T2 + + return { __t2: x.__t1 + ":foo_21" }; +>{ __t2: x.__t1 + ":foo_21" } : { __t2: string; } +>__t2 : string +>x.__t1 + ":foo_21" : string +>x.__t1 : string +>x : T1 +>__t1 : string +>":foo_21" : string +} + +var x3 = f1() +>x3 : Promise<{ __t3: string; }> +>f1() .then(f2, (e: Error) => { throw e;}) .then((x: T2) => { return { __t3: x.__t2 + "bar" };}) : Promise<{ __t3: string; }> +>f1() .then(f2, (e: Error) => { throw e;}) .then : { (onfulfilled?: (value: T2) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): Promise; (onfulfilled?: (value: T2) => TResult | PromiseLike, onrejected?: (reason: any) => void): Promise; } +>f1() .then(f2, (e: Error) => { throw e;}) : Promise +>f1() .then : { (onfulfilled?: (value: T1) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): Promise; (onfulfilled?: (value: T1) => TResult | PromiseLike, onrejected?: (reason: any) => void): Promise; } +>f1() : Promise +>f1 : () => Promise + + .then(f2, (e: Error) => { +>then : { (onfulfilled?: (value: T1) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): Promise; (onfulfilled?: (value: T1) => TResult | PromiseLike, onrejected?: (reason: any) => void): Promise; } +>f2 : (x: T1) => T2 +>(e: Error) => { throw e;} : (e: Error) => void +>e : Error +>Error : Error + + throw e; +>e : Error + +}) + .then((x: T2) => { +>then : { (onfulfilled?: (value: T2) => TResult | PromiseLike, onrejected?: (reason: any) => TResult | PromiseLike): Promise; (onfulfilled?: (value: T2) => TResult | PromiseLike, onrejected?: (reason: any) => void): Promise; } +>(x: T2) => { return { __t3: x.__t2 + "bar" };} : (x: T2) => { __t3: string; } +>x : T2 +>T2 : T2 + + return { __t3: x.__t2 + "bar" }; +>{ __t3: x.__t2 + "bar" } : { __t3: string; } +>__t3 : string +>x.__t2 + "bar" : string +>x.__t2 : string +>x : T2 +>__t2 : string +>"bar" : string + +}); diff --git a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt index b963b2c33aedb..4960940d3ab0b 100644 --- a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt +++ b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt @@ -1,16 +1,18 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(12,10): error TS2339: Property 'bar' does not exist on type 'A'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(33,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(34,10): error TS2339: Property 'bar' does not exist on type 'B'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(63,10): error TS2339: Property 'bar2' does not exist on type 'C1'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(82,10): error TS2339: Property 'bar' does not exist on type 'D'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(109,10): error TS2339: Property 'bar2' does not exist on type 'E1'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(131,11): error TS2339: Property 'foo' does not exist on type 'string | F'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(132,11): error TS2339: Property 'bar' does not exist on type 'string | F'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(157,11): error TS2339: Property 'foo2' does not exist on type 'G1'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(179,11): error TS2339: Property 'bar' does not exist on type 'H'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(65,10): error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(66,10): error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(85,10): error TS2339: Property 'bar' does not exist on type 'D'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(111,10): error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(112,10): error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(134,11): error TS2339: Property 'foo' does not exist on type 'string | F'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(135,11): error TS2339: Property 'bar' does not exist on type 'string | F'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(160,11): error TS2339: Property 'foo2' does not exist on type 'G1'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(182,11): error TS2339: Property 'bar' does not exist on type 'H'. -==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (10 errors) ==== +==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (12 errors) ==== interface AConstructor { new (): A; } @@ -67,21 +69,26 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru } interface C1 { foo: string; + c: string; bar1: number; } interface C2 { foo: string; + c: string; bar2: number; } declare var C: CConstructor; var obj5: C1 | A; - if (obj5 instanceof C) { // narrowed to C1. + if (obj5 instanceof C) { // narrowed to C1|C2. obj5.foo; + obj5.c; obj5.bar1; + ~~~~ +!!! error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. obj5.bar2; ~~~~ -!!! error TS2339: Property 'bar2' does not exist on type 'C1'. +!!! error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. } var obj6: any; @@ -126,12 +133,14 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru declare var E: EConstructor; var obj9: E1 | A; - if (obj9 instanceof E) { // narrowed to E1. + if (obj9 instanceof E) { // narrowed to E1 | E2 obj9.foo; obj9.bar1; + ~~~~ +!!! error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. obj9.bar2; ~~~~ -!!! error TS2339: Property 'bar2' does not exist on type 'E1'. +!!! error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. } var obj10: any; diff --git a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.js b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.js index e5b12bacb1f3a..7e6b332447062 100644 --- a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.js +++ b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.js @@ -49,17 +49,20 @@ interface CConstructor { } interface C1 { foo: string; + c: string; bar1: number; } interface C2 { foo: string; + c: string; bar2: number; } declare var C: CConstructor; var obj5: C1 | A; -if (obj5 instanceof C) { // narrowed to C1. +if (obj5 instanceof C) { // narrowed to C1|C2. obj5.foo; + obj5.c; obj5.bar1; obj5.bar2; } @@ -104,7 +107,7 @@ interface E2 { declare var E: EConstructor; var obj9: E1 | A; -if (obj9 instanceof E) { // narrowed to E1. +if (obj9 instanceof E) { // narrowed to E1 | E2 obj9.foo; obj9.bar1; obj9.bar2; @@ -213,6 +216,7 @@ if (obj4 instanceof B) { var obj5; if (obj5 instanceof C) { obj5.foo; + obj5.c; obj5.bar1; obj5.bar2; } diff --git a/tests/cases/compiler/narrowTypeByInstanceof.ts b/tests/cases/compiler/narrowTypeByInstanceof.ts new file mode 100644 index 0000000000000..6fe6693dd96d8 --- /dev/null +++ b/tests/cases/compiler/narrowTypeByInstanceof.ts @@ -0,0 +1,24 @@ + class Match { + public range(): any { + return undefined; + } + } + + class FileMatch { + public resource(): any { + return undefined; + } + } + +type FileMatchOrMatch = FileMatch | Match; + + +let elementA: FileMatchOrMatch, elementB: FileMatchOrMatch; + +if (elementA instanceof FileMatch && elementB instanceof FileMatch) { + let a = elementA.resource().path; + let b = elementB.resource().path; +} else if (elementA instanceof Match && elementB instanceof Match) { + let a = elementA.range(); + let b = elementB.range(); +} diff --git a/tests/cases/compiler/promiseVoidErrorCallback.ts b/tests/cases/compiler/promiseVoidErrorCallback.ts new file mode 100644 index 0000000000000..3169288f79a3a --- /dev/null +++ b/tests/cases/compiler/promiseVoidErrorCallback.ts @@ -0,0 +1,28 @@ +//@target: ES6 +interface T1 { + __t1: string; +} + +interface T2 { + __t2: string; +} + +interface T3 { + __t3: string; +} + +function f1(): Promise { + return Promise.resolve({ __t1: "foo_t1" }); +} + +function f2(x: T1): T2 { + return { __t2: x.__t1 + ":foo_21" }; +} + +var x3 = f1() + .then(f2, (e: Error) => { + throw e; +}) + .then((x: T2) => { + return { __t3: x.__t2 + "bar" }; +}); \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts index fee92ff10cac2..b81dd26652b0e 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts @@ -48,17 +48,20 @@ interface CConstructor { } interface C1 { foo: string; + c: string; bar1: number; } interface C2 { foo: string; + c: string; bar2: number; } declare var C: CConstructor; var obj5: C1 | A; -if (obj5 instanceof C) { // narrowed to C1. +if (obj5 instanceof C) { // narrowed to C1|C2. obj5.foo; + obj5.c; obj5.bar1; obj5.bar2; } @@ -103,7 +106,7 @@ interface E2 { declare var E: EConstructor; var obj9: E1 | A; -if (obj9 instanceof E) { // narrowed to E1. +if (obj9 instanceof E) { // narrowed to E1 | E2 obj9.foo; obj9.bar1; obj9.bar2;