From 4163d2dee4d78e26e831f9c369515fd1028d7cde Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 17 Jan 2019 17:16:52 -0800 Subject: [PATCH] Add tests --- .../inferFromGenericFunctionReturnTypes3.ts | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tests/cases/compiler/inferFromGenericFunctionReturnTypes3.ts diff --git a/tests/cases/compiler/inferFromGenericFunctionReturnTypes3.ts b/tests/cases/compiler/inferFromGenericFunctionReturnTypes3.ts new file mode 100644 index 00000000000..e44e7c4a5e0 --- /dev/null +++ b/tests/cases/compiler/inferFromGenericFunctionReturnTypes3.ts @@ -0,0 +1,174 @@ +// @strict: true +// @target: es6 +// @declaration: true + +// Repros from #5487 + +function truePromise(): Promise { + return Promise.resolve(true); +} + +interface Wrap { + value: T; +} + +function wrap(value: T): Wrap { + return { value }; +} + +function wrappedFoo(): Wrap<'foo'> { + return wrap('foo'); +} + +function wrapBar(value: 'bar'): Wrap<'bar'> { + return { value }; +} + +function wrappedBar(): Wrap<'bar'> { + const value = 'bar'; + const inferred = wrapBar(value); + const literal = wrapBar('bar'); + const value2: string = 'bar'; + const literal2 = wrapBar(value2); // Error + return wrap(value); +} + +function wrappedBaz(): Wrap<'baz'> { + const value: 'baz' = 'baz'; + return wrap(value); +} + +// Repro from #11152 + +interface FolderContentItem { + type: 'folder' | 'file'; +} + +let a: FolderContentItem[] = []; +a = [1, 2, 3, 4, 5].map(v => ({ type: 'folder' })); + +// Repro from #11312 + +let arr: Array<[number, number]> = [[1, 2]] + +let mappedArr: Array<[number, number]> = arr.map(([x, y]) => { + return [x, y]; +}) + +// Repro from #13594 + +export namespace DiagnosticSeverity { + export const Error = 1; + export const Warning = 2; + export const Information = 3; + export const Hint = 4; +} + +export type DiagnosticSeverity = 1 | 2 | 3 | 4; + +export interface Diagnostic { + severity?: DiagnosticSeverity; + code?: number | string; + source?: string; + message: string; +} + +function bug(): Diagnostic[] { + let values: any[] = []; + return values.map((value) => { + return { + severity: DiagnosticSeverity.Error, + message: 'message' + } + }); +} + +// Repro from #22870 + +function objectToMap(obj: any) { + return new Map(Object.keys(obj).map(key => [key, obj[key]])); +}; + +// Repro from #24352 + +interface Person { + phoneNumbers: { + __typename: 'PhoneNumber'; + }[]; +} + +function createPerson(): Person { + return { + phoneNumbers: [1].map(() => ({ + __typename: 'PhoneNumber' + })) + }; +} + +// Repro from #26621 + +type Box = { value: T }; +declare function box(value: T): Box; + +type WinCondition = + | { type: 'win', player: string } + | { type: 'draw' }; + +let zz: Box = box({ type: 'draw' }); + +type WinType = 'win' | 'draw'; + +let yy: Box = box('draw'); + +// Repro from #27074 + +interface OK { + kind: "OK"; + value: T; +} +export function ok(value: T): OK { + return { + kind: "OK", + value: value + }; +} + +let result: OK<[string, number]> = ok(["hello", 12]); + +// Repro from #25889 + +interface I { + code: 'mapped', + name: string, +} + +const a3: I[] = ['a', 'b'].map(name => { + return { + code: 'mapped', + name, + } +}); + +// Repro from https://www.memsql.com/blog/porting-30k-lines-of-code-from-flow-to-typescript/ + +type Player = { + name: string; + age: number; + position: "STRIKER" | "GOALKEEPER", +}; + +type F = () => Promise>; + +const f1: F = () => { + return Promise.all([ + { + name: "David Gomes", + age: 23, + position: "GOALKEEPER", + }, { + name: "Cristiano Ronaldo", + age: 33, + position: "STRIKER", + } + ]); +};