Skip to content

Commit

Permalink
add an extra test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Nov 22, 2023
1 parent 2be581e commit b549581
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 22 deletions.
40 changes: 40 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
controlFlowOptionalChain3.tsx(30,8): error TS18048: 'foo' is possibly 'undefined'.


==== controlFlowOptionalChain3.tsx (1 errors) ====
/// <reference path="/.lib/react16.d.ts" />

// https://github.com/microsoft/TypeScript/issues/56482

import React from "react";

interface Foo {
bar: boolean;
}

function test1(foo: Foo | undefined) {
if (foo?.bar === false) {
foo;
}
foo;
}

function test2(foo: Foo | undefined) {
if (foo?.bar === false) {
foo;
} else {
foo;
}
}

function Test3({ foo }: { foo: Foo | undefined }) {
return (
<div>
{foo?.bar === false && "foo"}
{foo.bar ? "true" : "false"}
~~~
!!! error TS18048: 'foo' is possibly 'undefined'.
</div>
);
}

71 changes: 51 additions & 20 deletions tests/baselines/reference/controlFlowOptionalChain3.symbols
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain3.ts] ////
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain3.tsx] ////

=== controlFlowOptionalChain3.tsx ===
/// <reference path="react16.d.ts" />

=== controlFlowOptionalChain3.ts ===
// https://github.com/microsoft/TypeScript/issues/56482

import React from "react";
>React : Symbol(React, Decl(controlFlowOptionalChain3.tsx, 4, 6))

interface Foo {
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.ts, 0, 0))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.tsx, 4, 26))

bar: boolean;
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.ts, 2, 15))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))
}

function test1(foo: Foo | undefined) {
>test1 : Symbol(test1, Decl(controlFlowOptionalChain3.ts, 4, 1))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 6, 15))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.ts, 0, 0))
>test1 : Symbol(test1, Decl(controlFlowOptionalChain3.tsx, 8, 1))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 10, 15))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.tsx, 4, 26))

if (foo?.bar === false) {
>foo?.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.ts, 2, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 6, 15))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.ts, 2, 15))
>foo?.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 10, 15))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))

foo;
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 10, 15))
}
foo;
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 10, 15))
}

function test2(foo: Foo | undefined) {
>test2 : Symbol(test2, Decl(controlFlowOptionalChain3.ts, 11, 1))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 13, 15))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.ts, 0, 0))
>test2 : Symbol(test2, Decl(controlFlowOptionalChain3.tsx, 15, 1))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 17, 15))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.tsx, 4, 26))

if (foo?.bar === false) {
>foo?.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.ts, 2, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 13, 15))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.ts, 2, 15))
>foo?.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 17, 15))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))

foo;
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 13, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 17, 15))

} else {
foo;
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.ts, 13, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 17, 15))
}
}

function Test3({ foo }: { foo: Foo | undefined }) {
>Test3 : Symbol(Test3, Decl(controlFlowOptionalChain3.tsx, 23, 1))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 25, 16))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 25, 25))
>Foo : Symbol(Foo, Decl(controlFlowOptionalChain3.tsx, 4, 26))

return (
<div>
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114))

{foo?.bar === false && "foo"}
>foo?.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 25, 16))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))

{foo.bar ? "true" : "false"}
>foo.bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))
>foo : Symbol(foo, Decl(controlFlowOptionalChain3.tsx, 25, 16))
>bar : Symbol(Foo.bar, Decl(controlFlowOptionalChain3.tsx, 6, 15))

</div>
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114))

);
}

44 changes: 42 additions & 2 deletions tests/baselines/reference/controlFlowOptionalChain3.types
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain3.ts] ////
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain3.tsx] ////

=== controlFlowOptionalChain3.tsx ===
/// <reference path="react16.d.ts" />

=== controlFlowOptionalChain3.ts ===
// https://github.com/microsoft/TypeScript/issues/56482

import React from "react";
>React : typeof React

interface Foo {
bar: boolean;
>bar : boolean
Expand Down Expand Up @@ -46,3 +51,38 @@ function test2(foo: Foo | undefined) {
}
}

function Test3({ foo }: { foo: Foo | undefined }) {
>Test3 : ({ foo }: { foo: Foo | undefined; }) => JSX.Element
>foo : Foo | undefined
>foo : Foo | undefined

return (
>( <div> {foo?.bar === false && "foo"} {foo.bar ? "true" : "false"} </div> ) : JSX.Element

<div>
><div> {foo?.bar === false && "foo"} {foo.bar ? "true" : "false"} </div> : JSX.Element
>div : any

{foo?.bar === false && "foo"}
>foo?.bar === false && "foo" : false | "foo"
>foo?.bar === false : boolean
>foo?.bar : boolean | undefined
>foo : Foo | undefined
>bar : boolean | undefined
>false : false
>"foo" : "foo"

{foo.bar ? "true" : "false"}
>foo.bar ? "true" : "false" : "false" | "true"
>foo.bar : boolean
>foo : Foo | undefined
>bar : boolean
>"true" : "true"
>"false" : "false"

</div>
>div : any

);
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// @strict: true
// @noEmit: true
// @esModuleInterop: true
// @jsx: react

/// <reference path="/.lib/react16.d.ts" />

// https://github.com/microsoft/TypeScript/issues/56482

import React from "react";

interface Foo {
bar: boolean;
}
Expand All @@ -21,3 +27,12 @@ function test2(foo: Foo | undefined) {
foo;
}
}

function Test3({ foo }: { foo: Foo | undefined }) {
return (
<div>
{foo?.bar === false && "foo"}
{foo.bar ? "true" : "false"}
</div>
);
}

0 comments on commit b549581

Please sign in to comment.