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 — [Master] Type checking JSX children by yuit · Pull Request #15160 · microsoft/TypeScript Conversation
|
| IntrinsicClassAttributes: "IntrinsicClassAttributes" | ||
| }; | ||
|
|
||
| const jsxChildrenPropertyName = "children"; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
i would say we should treat this the same way we do with props instead of hard coding it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean { | ||
| if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) { | ||
| const isComparingJsxAttributes = !!(source.flags & TypeFlags.JsxAttributes); | ||
| const containsSynthesizedJsxChildren = !!(source.flags & TypeFlags.ContainsSynthesizedJsxChildren); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
do not think we need this here
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| "category": "Error", | ||
| "code": 2707 | ||
| }, | ||
| "props.children are specified twice. The attribute named 'children' will be overwritten.": { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
name for children should be variable, and i would not include props here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
… error if you use children without specified such property
# Conflicts: # src/compiler/checker.ts # src/compiler/diagnosticMessages.json
|
ping Ryan Cavanaugh (@RyanCavanaugh) Mohamed Hegazy (@mhegazy) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| let _jsxNamespace: string; | ||
| let _jsxFactoryEntity: EntityName; | ||
| let _jsxElementAttribPropInterfaceSymbol: Symbol; // JSX.ElementAttributesProperty [symbol] |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
A comment would be good here
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
(A longer one explaining it, that is)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // interface ElementAttributesProperty { | ||
| // props: { | ||
| // children?: any; | ||
| // }; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Let's figure out some other way to do this - this is too opaque and might not combine well with future work
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| error(attribsPropTypeSym.declarations[0], Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, JsxNames.ElementAttributesPropertyNameContainer); | ||
| return undefined; | ||
| // No interface exists, so the element attributes type will be an implicit any | ||
| _jsxElementPropertiesName = undefined; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this mean we'll keep doing this logic repeatedly?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yep if we can't find the JSXElement. May be it is better to be empty string to indicate that we already done this
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // If the call has correct arity, we will then check if the argument type and parameter type is assignable | ||
|
|
||
| const callIsIncomplete = node.attributes.end === node.end; // If we are missing the close "/>", the call is incomplete | ||
| const callIsIncomplete = node.attributes.end === node.end; // If we are missing the close "/>", the call is incoplete |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Undo spelling regression
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| export function isWhiteSpace(ch: number): boolean { | ||
| export function isWhiteSpaceLike(ch: number): boolean { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why the rename?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Because it is not just WhiteSpace but Whitespace and empty line.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GTG after lint fixes |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks Ryan Cavanaugh (@RyanCavanaugh) for reviewing 🍰 🍰 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you! This is a great feature. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Yui (@yuit) can you write up some minimal docs? I can fill in some details but would like you to write down the basics |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
The issue that this is meant to resolve has examples that restrict the type of the components in children, but I don't see an example in this PR that does that. Is it actually possible? For example, in the linked issue there is a a use-case where a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I see you've also been checking on SO: https://stackoverflow.com/questions/44475309/how-do-i-restrict-the-type-of-react-children-in-typescript-using-the-newly-adde I think that the issue here is that all you really get back from a rendered component is a JSX Element. There's not really any information in thre to specify where the element came from. I'm not sure if Yui (@yuit) or Ryan Cavanaugh (@RyanCavanaugh) have had any thoughts on this. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
yeah, I have become skeptical that what I'm trying to do is possible, which makes me think that #13618 should not have been closed, since it is specifically about getting type restrictions on what kind of components are used as children of a react component. But I could very well be missing something! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Yeah, this is what I saw in my tests, too. You can't say something like this: import React, { Component } from 'react'; import { render } from 'react-dom'; class Foo extends Component<{}, void> { render() { return <p>Hello from Foo!</p>; } } interface BarProps { children?: Foo | Array<Foo>; } const Bar = ({ children }: BarProps) => <div>This must be Foo(s): {children}.</div>; render( <div> <Bar> <Foo /> {/* invalid - is treated as `JSX.Element`, not `Foo` */} </Bar> </div>, document.getElementById('app') ); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
If this really can't be done, then I suggest re-opening #13618, since it's specifically about typing the components used as children (should I comment as such there?) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
any "last words" on this folks ?
cc Yui (@yuit) Daniel Rosenwasser (@DanielRosenwasser) Ryan Cavanaugh (@RyanCavanaugh) thx! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Would love to see support for typing children! 👍 |
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.