Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to store and reuse result of user defined type guard function #24983

Closed
rssfrncs opened this issue Jun 15, 2018 · 3 comments
Closed

Unable to store and reuse result of user defined type guard function #24983

rssfrncs opened this issue Jun 15, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@rssfrncs
Copy link

rssfrncs commented Jun 15, 2018

TypeScript Version: 2.9.1

Search Terms: caching type guard result

Code

type TChildrenFunc = () => string;
type TChildren = TChildrenFunc | string;

function isFunction(render: TChildren): render is TChildrenFunc {
  return typeof render === "function";
}

// render is of type TChildren
const cacheResult = isFunction(render);

// errors below
const test = cacheResult ? render() : render;

Expected behavior:

Same as in-lining the type guard call.

Actual behavior:

TypeScript is unable to infer the correct type.

Playground Link: https://codesandbox.io/s/30v146k811 Check Hello.tsx

Related Issues: Nope.

@rssfrncs rssfrncs changed the title Unable to cache result of user defined type guard function Unable to store and reuse result of user defined type guard function Jun 15, 2018
@hraban
Copy link

hraban commented Jun 15, 2018

I just came here to post a similar bug:

function twostep(x: symbol | string) {
    const isSymbol = typeof x === "symbol";
    if (!isSymbol) {
        console.log(`Type of x is still string | symbol here: ${x}`);
    }
}

function onestep(x: symbol | string) {
    if (typeof x !== "symbol") {
        console.log(`Type of x is now just string: ${x}`);
    }
}

playground

@mhegazy
Copy link
Contributor

mhegazy commented Jun 19, 2018

Looks like a duplicate of #24865

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 19, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants