GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Support LibraryManagedAttributes<TComponent, TAttributes> JSX namespace type by weswigham · Pull Request #24422 · microsoft/TypeScript Conversation
|
|
Could you elaborate in this PR on what |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| & {[K in Exclude<keyof TProps, keyof TDefaults>]: TProps[K]} | ||
| & Partial<TDefaults>; | ||
|
|
||
| type InferedPropTypes<P> = {[K in keyof P]: P[K] extends PropTypeChecker<infer T, infer U> ? PropTypeChecker<T, U>[typeof checkedType] : {}}; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I know it's just a test, but InferedPropTypes -> InferredPropTypes
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const x = <JustDefaultPropsWithSpecifiedGeneric foo="eh" />; | ||
| const y = <JustDefaultPropsWithSpecifiedGeneric foo="no" bar="ok" />; // error, no prop named bar | ||
| const z = <JustDefaultPropsWithSpecifiedGeneric foo={12} />; // error, wrong type | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What about <JustDefaultPropsWithSpecifiedGeneric />? Is that kosher?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
As background, TS works differently than Flow at the moment with React Beforeinterface Props { thing?: string; // thing is optional, but it is always defined because it has a default prop. } class Thing extends React.Component<Props> { static defaultProps = { thing: 'hello' } render() { console.log(this.props.thing!.length) // forced with ! // ... } } // .. <Thing /> // this works Afterinterface Props { thing?: string; // still optional } class Thing extends React.Component<Props> { static defaultProps = { thing: 'hello' } render() { console.log(this.props.thing) // inferred because of defaultProps, no ! needed // ... } } // .. <Thing /> // works |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Jared Palmer (@jaredpalmer) The interface Props { thing?: string; // still optional } class Thing extends React.Component<Props> { static defaultProps = { thing: 'hello' } render() { console.log(this.props.thing) // inferred because of defaultProps, no ! needed // ... } } // .. <Thing /> // works you'd have interface Props { thing: string; // note: not optional within class, but is optional when writing due to the default } class Thing extends React.Component<Props> { static defaultProps = { thing: 'hello' } render() { console.log(this.props.thing) // strict type from `Props` // ... } } // .. <Thing /> // works, thanks to `defaultProps` or at least that's what makes sense to me - since the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
got it so it will work like Flow. cool! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This is indeed awesome! In the meantime I wrote an article for React consumers struggling with this "deal breaker" ✌️ https://medium.com/@martin_hotell/react-typescript-and-defaultprops-dilemma-ca7f81c661c7 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| type Defaultize<TProps, TDefaults> = | ||
| & {[K in Extract<keyof TProps, keyof TDefaults>]?: TProps[K]} | ||
| & {[K in Exclude<keyof TProps, keyof TDefaults>]: TProps[K]} | ||
| & Partial<TDefaults>; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why is the Partial<TDefaults> here intersected with the extracted mapped type from TDefaults? What kind of error does that produce if typeof TProps[K] is incompatible with typeof TDefaults[K]?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Mohamed Hegazy (@mhegazy) I've synchronized this branch with |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
We will need to update https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/JSX.md |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
What is the expected behaviour when the props type is a union? Is this expected? { type Props = { shared: string } & ( | { type: 'foo'; foo: string; } | { type: 'bar'; bar: string; }); class Component extends ReactComponent<Props> { static defaultProps = { shared: 'yay', }; } <Component type="foo" />; // Expected error, got none // Unexpected error // Property 'foo' does not exist on type 'Defaultize<Props, { shared: string; }>'. <Component type="foo" foo="1" />; } |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Oliver Joseph Ash (@OliverJAsh) this is a different issue i am afraid. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Mohamed Hegazy (@mhegazy) Is it an issue worth tracking? If so, I'll open a separate issue. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
yes please. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Oliver Joseph Ash (@OliverJAsh) it's probably also worth noting that while this code is in |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Locking so users open up new issues instead. That way, we can more easily follow up. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.