Comments (9)
Don't know how that label got added - I haven't gotten the chance to actually look at this issue. My apologies.
from typescript.
from typescript.
@RyanCavanaugh yes but that error doesn't appear any more, only the 2604 one
from typescript.
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.
I'm a bit lost - so you're saying that this polymorphic behavior should never have worked and cannot work?
from typescript.
BTW in the original implementation if C=="Baz"
then the props would be the ones from span
from typescript.
@RyanCavanaugh I'm not sure what to do here - is a polymorphic component impossible, or are we doing it wrongly?
from typescript.
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.
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)
- Add persian translation to docs HOT 1
- No error when accessing setter-only properties HOT 2
- Preserve `set`-only accessors in declaration emit
- NoInfer introduced in 5.4 is undocumented HOT 5
- Compile does not produce an error when parameters in overriding class method in child class have narrower type than the parameters of this method in the parent class HOT 4
- Update getTimezoneOffset JSDoc for clarity HOT 1
- ts80007 is incorrect at times? HOT 3
- Problems with __esModule marker in JS/TS mixed node16 environemnt HOT 3
- Error reported only in `node_modules` directory HOT 3
- Incorrect Chinese Translation for 'initializer' and Incorrect Use of Chinese Quotation Marks
- Shadowed import bindings require cross-file knowledge even with `isolatedModules` HOT 4
- Hover references/docs for JSX files (like for regular HTML and CSS files) HOT 7
- `noImplicitAny` infers `any` for typed parameter of union's common function HOT 2
- Wrapper Function that Runs Generic Function Doesn't Honor Generic Function Return Type HOT 2
- Memory Leak disposing oldProgram HOT 2
- A class with only getters satisfies some type without checking for the possibility of changing these fields HOT 2
- "Output generation failed" when transpiling "test.d.abc.ts" HOT 4
- Type literals in declaration files are emitted with comment blocks from the wrong source file
- Declaration emit of private properties should strip jsdoc
- Typescript enum suggestion Chinese garbled code HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.