Skip to content

Typescript 5.0.2 does not infer type correctly with React.isValidElement #53348

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
changwoolab opened this issue Mar 19, 2023 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@changwoolab
Copy link

changwoolab commented Mar 19, 2023

Bug Report

import React from "react";

type B = {
  b1: string;
  b2: number;
};

export function type(input: React.ReactElement | string | B) {
  if (typeof input === "string") {
    return input;
  }

  if (React.isValidElement(input)) {
    return input;
  }

  return input.b1;
}

After running tsc in the console, I got this error.

Screenshot 2023-03-19 at 5 27 09 PM

But there was no error in VScode and type inference was working well
Screenshot 2023-03-19 at 5 29 43 PM

Also, there was no error with 4.9.4.

🔎 Search Terms

typescript 5.0, React, ReactElement, isValidElement, inference, deduce

🕗 Version & Regression Information

After upgrading to typescript 5.0.2 and running tsc in the console.

  • This is a crash
  • This changed between versions 4.9.4 and 5.0.2
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

import React from "react";

type B = {
  b1: string;
  b2: number;
};

export function type(input: React.ReactElement | string | B) {
  if (typeof input === "string") {
    return input;
  }

  if (React.isValidElement(input)) {
    return input;
  }

  return input.b1;
}

🙁 Actual behavior

Type of input at the last line was B | ReactElement

🙂 Expected behavior

Type of input at the last line should be B

@changwoolab changwoolab changed the title Typescript 5.0.2 does not deduce type correctly Typescript 5.0.2 does not infer type correctly Mar 19, 2023
@changwoolab changwoolab changed the title Typescript 5.0.2 does not infer type correctly Typescript 5.0.2 does not infer type correctly with React.isValidElement Mar 19, 2023
@MartinJohns
Copy link
Contributor

This seems to be a duplicate of #53178.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 31, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants