GithubHelp home page GithubHelp logo

Comments (9)

DanielRosenwasser avatar DanielRosenwasser commented on April 27, 2024 1

Don't know how that label got added - I haven't gotten the chance to actually look at this issue. My apologies.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on April 27, 2024

#57117 (comment)

from typescript.

wmertens avatar wmertens commented on April 27, 2024

@RyanCavanaugh yes but that error doesn't appear any more, only the 2604 one

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on April 27, 2024

This is, for all intents and purposes, a correct error that was missed in 5.3. Prior versions were incorrectly (within the bounds of correct analysis that actually exist) allowing the call, though I'm still not exactly sure why.

Further reduced (bisect on this one is still #56004):

declare const React: any;
namespace JSX {
  export interface IntrinsicElements {
    div: {
      name: string;
      as?: "div";
    };
  }
}

// This is required for the issue, changing it to return T makes it a different error
type ObjectProps<T> = T extends Record<any, any> ? T : never;
type FunctionComponent<P> = (props: P) => any;
// string|fn is required for the issue, changing it to only check for fn makes it a different error
type PropsOf<COMP> = COMP extends keyof JSX.IntrinsicElements
  ? JSX.IntrinsicElements[COMP]
  : COMP extends FunctionComponent<infer OrigProps>
  ? ObjectProps<OrigProps & Record<any, any>>
  : never

const Poly1 = <C extends string | FunctionComponent<unknown>>(thing: { as: C } & PropsOf<C>) => {
  const Cmp = thing.as;
  const p1 = <Cmp {...(null as any)} />;;
  if (typeof Cmp === "string") {
    const p2 = <Cmp {...(null as any)} />;
  } else {
    const p3 = <Cmp {...(null as any)} />;
  }
}

Poly1({ as: "div", name: "foo" });

In 5.3, p2 is an error, but p1 isn't. That doesn't make any sense -- if p2 is an error, then p1 has to be one too. In 5.4 they're consistently both errors.

Correct analysis of this function basically boils down to being able to reason about the fact that

Poly1<"baz">(...);

can't actually happen since if C = baz then props = never, but TS has never had that capability.

from typescript.

wmertens avatar wmertens commented on April 27, 2024

I'm a bit lost - so you're saying that this polymorphic behavior should never have worked and cannot work?

from typescript.

wmertens avatar wmertens commented on April 27, 2024

BTW in the original implementation if C=="Baz" then the props would be the ones from span

from typescript.

wmertens avatar wmertens commented on April 27, 2024

@RyanCavanaugh I'm not sure what to do here - is a polymorphic component impossible, or are we doing it wrongly?

from typescript.

wmertens avatar wmertens commented on April 27, 2024

Ok - with some inspiration here I managed to make it pass in newer TS versions, will merge soon. Since I understand that it was failing correctly, I'll close this issue.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on April 27, 2024

Sorry for the delay btw, we were double-checking a few other things to make sure this wasn't secretly a bug of some kind. Glad to hear you got an acceptable solution on your end.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.