Skip to content

Runtime Validation With Variables Fails #48241

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

Closed
GEMISIS opened this issue Mar 13, 2022 · 8 comments
Closed

Runtime Validation With Variables Fails #48241

GEMISIS opened this issue Mar 13, 2022 · 8 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@GEMISIS
Copy link

GEMISIS commented Mar 13, 2022

Bug Report

🔎 Search Terms

typescript validation variable boolean failure

🕗 Version & Regression Information

Version 4.6.2 and below all seem to exhibit the same issue. Also confirmed most recent nightly build as of this filing exhibits the same issue.

This is the behavior in every version I tried, and I reviewed the FAQ for entries about TypeScript type validation

⏯ Playground Link

Playground link with relevant code

💻 Code

  interface test {
      val1: string | undefined;
  }

  function test1(input: string) {
      console.log(input);
  }
  function test2(input: test) {
      const canWork = input.val1 !== undefined;
      // Fails
      if (canWork) {
          test1(input.val1);
      }
      // Succeeds
      if (input.val1 !== undefined) {
          test1(input.val1);
      }
  }

🙁 Actual behavior

Receive the following error for type validation:

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.(2345)

🙂 Expected behavior

Type validation from the boolean expression should work.

@MartinJohns
Copy link
Contributor

This is working as intended. See #44730.

Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, and the reference being narrowed is a const variable, a readonly property, or a parameter for which there are no assignments in the function body.

@GEMISIS
Copy link
Author

GEMISIS commented Mar 13, 2022

@MartinJohns Got it, that's super strange and not what I would have expected, as it seems similar to the f6 function in #44730, but I guess the compiler doesn't know which type to choose still/can't pick one?

@fatcerberus
Copy link

fatcerberus commented Mar 14, 2022

Look closer: f6 in #44730 is explicitly an example of a function where narrowing doesn’t work.

Some examples where narrowing does not occur (but it would had the indirectly referenced conditions been written in-line)

@GEMISIS
Copy link
Author

GEMISIS commented Mar 14, 2022

@fatcerberus I meant closer in terms of removing the obj reassignment in-function. IE: If it was like this:

function f6(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) {
    const isFoo = obj.kind === 'foo';
    if (isFoo) {
        obj.foo;  // Error, no narrowing because 'obj' is assigned in function body
    }
}

It seems that because obj has two distinct definitions, this is why it works? So in my example, if test was defined as:

interface test {
    val1: string;
} | {
    val1: undefined;
}

Then everything works fine, which is weird to me was my point.

@fatcerberus
Copy link

Ah, that's a bit tricky, but I suspect that works because obj.kind === 'foo' is treated as a type guard applying to obj itself (because it's a discriminated union), rather than only obj.kind. Thus it meets the criteria of directly narrowing a parameter which is not reassigned. In your example you're trying to narrow a single property of an object (input.val1), not input as a whole, which fails because val1 is not declared readonly.

@MartinJohns
Copy link
Contributor

That'd be my best guess as well.

@RyanCavanaugh
Copy link
Member

We're quite a bit stricter about when to apply indirect narrowing as opposed to indirect narrowing. If you mark it as readonly val1: string | undefined then it succeeds as expected.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 14, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants