diff --git a/tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts b/tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts new file mode 100644 index 00000000000..169dbc7a7c8 --- /dev/null +++ b/tests/cases/conformance/controlFlow/typeGuardsTypeParameters.ts @@ -0,0 +1,35 @@ +// @strictNullChecks: true + +// Type guards involving type parameters produce intersection types + +class C { + prop: string; +} + +function f1(x: T) { + if (x instanceof C) { + let v1: T = x; + let v2: C = x; + x.prop; + } +} + +function f2(x: T) { + if (typeof x === "string") { + let v1: T = x; + let v2: string = x; + x.length; + } +} + +// Repro from #13872 + +function fun(item: { [P in keyof T]: T[P] }) { + const strings: string[] = []; + for (const key in item) { + const value = item[key]; + if (typeof value === "string") { + strings.push(value); + } + } +}