Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Nov 29, 2019
1 parent 3d0217b commit 9b9ba78
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3865,7 +3865,7 @@ namespace ts {
}
if (type.flags & TypeFlags.TypeParameter || objectFlags & ObjectFlags.ClassOrInterface) {
if (type.flags & TypeFlags.TypeParameter && contains(context.inferTypeParameters, type)) {
context.approximateLength += (symbolName(type.symbol).length + 6);
context.approximateLength += 6 + (type.symbol ? symbolName(type.symbol).length : 1);
return createInferTypeNode(typeParameterToDeclarationWithConstraint(type as TypeParameter, context, /*constraintNode*/ undefined));
}
if (context.flags & NodeBuilderFlags.GenerateNamesForShadowedTypeParams &&
Expand Down Expand Up @@ -4801,7 +4801,7 @@ namespace ts {
return cached;
}
}
let result = symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ true);
let result = type.symbol ? symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ true) : createIdentifier("?");
if (!(result.kind & SyntaxKind.Identifier)) {
return createIdentifier("(Missing type parameter)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>fn : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>fn : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>fn : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>async () => await this : () => Promise<this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this>
>await this : this extends undefined ? this : this extends PromiseLike<infer ?> ? ? : this
>this : this
}
}
Expand Down
17 changes: 16 additions & 1 deletion tests/baselines/reference/asyncFunctionReturnType.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
}

// #27711

async function fGeneric<T>(x: T) {
return x;
}
const expected: Promise<number> = fGeneric(undefined as Promise<number>);


//// [asyncFunctionReturnType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
Expand Down Expand Up @@ -172,3 +180,10 @@ function fGenericIndexedTypeForExplicitPromiseOfKProp(obj, key) {
return Promise.resolve(obj[key]);
});
}
// #27711
function fGeneric(x) {
return __awaiter(this, void 0, void 0, function* () {
return x;
});
}
const expected = fGeneric(undefined);
19 changes: 19 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,22 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>obj : Symbol(obj, Decl(asyncFunctionReturnType.ts, 72, 100))
>key : Symbol(key, Decl(asyncFunctionReturnType.ts, 72, 110))
}

// #27711

async function fGeneric<T>(x: T) {
>fGeneric : Symbol(fGeneric, Decl(asyncFunctionReturnType.ts, 74, 1))
>T : Symbol(T, Decl(asyncFunctionReturnType.ts, 78, 24))
>x : Symbol(x, Decl(asyncFunctionReturnType.ts, 78, 27))
>T : Symbol(T, Decl(asyncFunctionReturnType.ts, 78, 24))

return x;
>x : Symbol(x, Decl(asyncFunctionReturnType.ts, 78, 27))
}
const expected: Promise<number> = fGeneric(undefined as Promise<number>);
>expected : Symbol(expected, Decl(asyncFunctionReturnType.ts, 81, 5))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>fGeneric : Symbol(fGeneric, Decl(asyncFunctionReturnType.ts, 74, 1))
>undefined : Symbol(undefined)
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))

17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,20 @@ async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K
>obj : TObj
>key : K
}

// #27711

async function fGeneric<T>(x: T) {
>fGeneric : <T>(x: T) => Promise<T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T>
>x : T

return x;
>x : T
}
const expected: Promise<number> = fGeneric(undefined as Promise<number>);
>expected : Promise<number>
>fGeneric(undefined as Promise<number>) : Promise<number>
>fGeneric : <T>(x: T) => Promise<T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T>
>undefined as Promise<number> : Promise<number>
>undefined : undefined

2 changes: 1 addition & 1 deletion tests/baselines/reference/forAwaitForUnion.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async function f<T>(source: Iterable<T> | AsyncIterable<T>) {
>source : Iterable<T> | AsyncIterable<T>

for await (const x of source) {
>x : T
>x : T | (T extends undefined ? T : T extends PromiseLike<infer ?> ? ? : T)
>source : Iterable<T> | AsyncIterable<T>
}
}
Expand Down
9 changes: 8 additions & 1 deletion tests/cases/compiler/asyncFunctionReturnType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends

async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
}

// #27711

async function fGeneric<T>(x: T) {
return x;
}
const expected: Promise<number> = fGeneric(undefined as Promise<number>);

0 comments on commit 9b9ba78

Please sign in to comment.