From 6f2001fb237c47025f91c7d745d268a5fe428a6b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 2 Jun 2018 17:43:37 -0700 Subject: [PATCH] Add tests --- .../typeGuardOfFormTypeOfFunction.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfFunction.ts diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfFunction.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfFunction.ts new file mode 100644 index 0000000000..cbacf60ce6 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfFunction.ts @@ -0,0 +1,73 @@ + +function f1(x: any) { + if (typeof x === "function") { + x; // any + } +} + +function f2(x: unknown) { + if (typeof x === "function") { + x; // Function + } +} + +function f3(x: {}) { + if (typeof x === "function") { + x; // Function + } +} + +function f4(x: T) { + if (typeof x === "function") { + x; // T & Function + } +} + +function f5(x: { s: string }) { + if (typeof x === "function") { + x; // never + } +} + +function f6(x: () => string) { + if (typeof x === "function") { + x; // () => string + } +} + +function f10(x: string | (() => string)) { + if (typeof x === "function") { + x; // () => string + } + else { + x; // string + } +} + +function f11(x: { s: string } | (() => string)) { + if (typeof x === "function") { + x; // () => string + } + else { + x; // { s: string } + } +} + +function f12(x: { s: string } | { n: number }) { + if (typeof x === "function") { + x; // never + } + else { + x; // { s: string } | { n: number } + } +} + +// Repro from #18238 + +function f100(obj: T, keys: K[]) : void { + for (const k of keys) { + const item = obj[k]; + if (typeof item == 'function') + item.call(obj); + } +}