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)
- Type alias circularly references itself (5.4 regression) HOT 1
- error TS2385: Overload signatures must all be public, private or protected. HOT 2
- `export type * ...` statements in `.d.ts` files do not work (5.4 regression) HOT 4
- HTMLFormElement disallows symbol keys HOT 5
- TypeScript language service cannot find subclass references/implementation of mixin methods
- when using ts.getJSDocTags, the value of @type is not returned. Is there any solution? HOT 2
- Compiler allows narrower method signature than implemented interface HOT 7
- Allow overload signatures to have different access levels HOT 3
- Watcher recursively watches irrelevant directories HOT 11
- Auto-closing of tags within curly braces `{}` does not work when parent element is same tag in JSX HOT 1
- Undocumented breaking change in 5.5.0-beta for reference directives HOT 3
- 5.5.0 regression - importHelpers do not work with moduleResolution: bundler HOT 1
- 5.5.0 inheriting outDir: ${configDir} does not automatically exclude it from compilation HOT 3
- isolatedDeclarations wrongly enabled in VSCode HOT 3
- ESNext Set methods HOT 1
- isolatedDeclarations should not have warning for functions that have no return statements HOT 4
- Trivia ownership documentation seems to be incorrect
- `--isolatedDeclarations` allows generator functions HOT 1
- Allow `--noCheck` on the CLI with top-level `--build`
- Union in template literal simplifying unexpectedly HOT 2
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.