Title: Locally scoped & typed jsxFactory · Issue #44018 · microsoft/TypeScript · GitHub
Open Graph Title: Locally scoped & typed jsxFactory · Issue #44018 · microsoft/TypeScript
X Title: Locally scoped & typed jsxFactory · Issue #44018 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms jsx jsxFactory I found related tickets, but this ticket information is very long winded and I believe this requires its own ticket: #21699 #14729 #15217 #29376 ✅ Viability Checklist My suggestion meets these gui...
Open Graph Description: Suggestion 🔍 Search Terms jsx jsxFactory I found related tickets, but this ticket information is very long winded and I believe this requires its own ticket: #21699 #14729 #15217 #29376 ✅ Viability...
X Description: Suggestion 🔍 Search Terms jsx jsxFactory I found related tickets, but this ticket information is very long winded and I believe this requires its own ticket: #21699 #14729 #15217 #29376 ✅ Viability...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44018
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Locally scoped & typed jsxFactory","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\n\u003c!--\r\n 💡 Did you know? TypeScript has over 2,000 open suggestions!\r\n 🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.\r\n The \"Common Feature Requests\" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests\r\n \r\n \r\n\r\n Replace the text below:\r\n-->\r\n\r\njsx jsxFactory\r\n\r\nI found related tickets, but this ticket information is very long winded and I believe this requires its own ticket:\r\n- https://github.com/microsoft/TypeScript/issues/21699 \r\n- https://github.com/microsoft/TypeScript/issues/14729 \r\n- https://github.com/microsoft/TypeScript/issues/15217 \r\n- https://github.com/microsoft/TypeScript/issues/29376\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\r\n-->\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals). \r\n\r\n> This would be a type level only change, updating existing behaviour to be more inline with direct local scope types instead of reliance on global scope, for this reason, I believe all of the above items are true. \r\n\r\n## ⭐ Suggestion\r\n\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\n\r\nType completion from a defined `jsxFactory` function definition \r\n\r\n## 📃 Motivating Example\r\n\r\n\u003c!--\r\n If you were announcing this feature in a blog post, what's a short explanation that shows\r\n a developer why this feature improves the language?\r\n-->\r\n\r\nHere is a type definition covering the internal process that occurs within the defined `createNode` function in the same file. \r\n\r\nSample:\r\n```typescript\r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n \u003cTO extends O, S extends CreateNodeFragmentSourceFirstStage>(source: S, options?: TO, ...children: C[]): FragmentVNode & {\r\n source: S;\r\n options: TO;\r\n };\r\n}\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vnode/blob/8f4433f00852396027a73ed6711e8d1826bee5fc/src/create-node.ts#L46-L110)\r\n\r\nThe linked type definition allows for complex types to be defined, including static type resolution for leaves or scalar values:\r\n\r\n```typescript\r\nit.concurrent.each\u003c[SourceReference]>([\r\n [Symbol(\"Unique Symbol\")],\r\n [true],\r\n [false],\r\n [1],\r\n [0],\r\n [1n],\r\n [0n],\r\n [\"\"],\r\n [\"Hello!\"],\r\n])(\"%p should produce a scalar node\", async \u003cI extends SourceReference>(input: I ) => {\r\n const output = createNode(input);\r\n expect(isScalarVNode(output)).toEqual(true);\r\n const source: I = output.source;\r\n expect(source).toEqual(input);\r\n});\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vnode/blob/8f4433f00852396027a73ed6711e8d1826bee5fc/src/create-node.spec.ts#L42-L57)\r\n\r\nFrom the perspective of a consumer, all of the above values will produce an object that contains `source` that matches the original `input` value. These nodes will never produce children so the type definition accounts for this by using \r\n\r\n```typescript\r\n{\r\n children: never;\r\n}\r\n```\r\n\r\nThis is done by this section of the definition\r\n\r\n```typescript\r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n\u003cTO extends O, S extends SourceReference>(source: S): VNode & {\r\n source: S;\r\n options: never;\r\n scalar: true;\r\n children: never;\r\n };\r\n \u003cTO extends O, S extends SourceReference>(source: S, options?: TO): VNode & {\r\n source: S;\r\n options: TO;\r\n scalar: true;\r\n children: never;\r\n };\r\n \u003cTO extends O, S extends SourceReference>(source: S, options?: TO, ...children: C[]): VNode & {\r\n source: S;\r\n options: TO;\r\n scalar: false;\r\n };\r\n}\r\n```\r\n\r\nFrom a type perspective, the the values that are known by the caller are returned in the return type. We also get whether or not `children` is available (or the other side of this, whether the node is a `scalar` node, where no `children` were passed)\r\n\r\nGiven `jsxFactory` can be typed using this function, then `children` itself can also be typed \r\n\r\n`children` not typed:\r\n\r\n```typescript\r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n \u003cTO extends O, S extends CreateNodeFragmentSourceFirstStage>(source: S, options?: TO, ...children: C[]): \r\n FragmentVNode & {\r\n source: S;\r\n options: TO;\r\n }\r\n}\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vnode/blob/8f4433f00852396027a73ed6711e8d1826bee5fc/src/create-node.ts#L67-L70)\r\n\r\n`children` typed:\r\n\r\n```typescript\r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n \u003cTO extends O, S extends CreateNodeFragmentSourceFirstStage, TC extends C = C>(source: S, options?: TO, ...children: TC[]): FragmentVNode & {\r\n source: S;\r\n options: TO;\r\n children: AsyncIterable\u003cResolveNodeType\u003cTC>[]>;\r\n };\r\n}\r\n```\r\n\r\nGiven the above type was implemented, these types would be able to:\r\n\r\n- Know what children will be yielded at type level, meaning this extract this statically using ts-morph or another tool. \r\n- Restrict at compile time input state machines, if the input types are restricted for a node it could mean a full restriction in types throughout an entire structure. Typescript itself could eliminate code based on `never` alone\r\n- Given the previous point, at development time this can be utilised while typing to improve some dev ex as side processes could be emulating these restrictions. \r\n\r\nWhile the above code is scalar values, the static leaves of a state tree, functions can be fully typed as well... only if the first point is true. \r\n\r\nWhile it would make this definition vastly more complex, I believe it still is possible from a pure type perspective\r\n\r\nCurrently a function returns a fragment node, which produces only state through `children`.\r\n\r\n\r\n```typescript\r\nit.concurrent.each\u003c[CreateNodeFragmentSourceFirstStage]>([\r\n [() => {}],\r\n [Promise.resolve()],\r\n [Fragment],\r\n])(\"%p should produce a fragment node\", async (input) => {\r\n const output: FragmentVNode = createNode(input);\r\n expect(isFragmentVNode(output)).toEqual(true);\r\n});\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vnode/blob/8f4433f00852396027a73ed6711e8d1826bee5fc/src/create-node.spec.ts#L30)\r\n\r\nThe matching types for functions and promises currently drop the type of `children` once passed. \r\n\r\n```typescript\r\nexport type CreateNodeFragmentSourceFirstStage =\r\n | Function\r\n | Promise\u003cunknown>\r\n | typeof Fragment;\r\n \r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n \u003cTO extends O, S extends CreateNodeFragmentSourceFirstStage>(source: S, options?: TO, ...children: C[]): \r\n FragmentVNode & {\r\n source: S;\r\n options: TO;\r\n };\r\n}\r\n```\r\n\r\nThe retain these we need to type `children` only additionally:\r\n\r\n```typescript\r\nexport type CreateNodeFragmentSourceFirstStage =\r\n | Function\r\n | Promise\u003cunknown>\r\n | typeof Fragment;\r\n \r\ninterface CreateNodeFn\u003c\r\n O extends object = object,\r\n S = Source\u003cO>,\r\n C extends VNodeRepresentationSource = VNodeRepresentationSource\r\n> {\r\n \u003cTO extends O, S extends CreateNodeFragmentSourceFirstStage, TC extends C = C>(source: S, options?: TO, ...children: TC[]): FragmentVNode & {\r\n source: S;\r\n options: TO;\r\n children: AsyncIterable\u003cResolveNodeType\u003cTC>[]>;\r\n };\r\n}\r\n```\r\n\r\nBecause the types of all children nodes will be typed by the time they are defined as vnodes, all types will be complete. \r\n\r\nFrom a top level at this point we would be able to statically resolve a complete type set from a tree of components. \r\n\r\n## 💻 Use Cases\r\n\r\n\u003c!--\r\n What do you want to use this for?\r\n What shortcomings exist with current approaches?\r\n What workarounds are you using in the meantime?\r\n-->\r\n\r\n### Local `string` types:\r\n\r\nhttps://github.com/microsoft/TypeScript/issues/15217\r\n\r\n### Imported `string | number | bigint | symbol | boolean` types:\r\n\r\n`vnode` defines \"token\" types, which allows definition of a partial component with no implementation. \r\n\r\n```typescript\r\n\u003cOfferCatalog>\r\n \u003cProduct\r\n name=\"This is my name 1\"\r\n sku=\"SKU 123\"\r\n >\r\n \u003cBrand name=\"Some brand\" />\r\n \u003c/Product>\r\n \u003cProduct\r\n name=\"This is my name 2\"\r\n sku=\"SKU 124\"\r\n >\r\n \u003cBrand name=\"Some brand\" />\r\n \u003c/Product>\r\n \u003cProduct\r\n name=\"This is my name 3\"\r\n sku=\"SKU 125\"\r\n >\r\n \u003cBrand name=\"Some other brand\" />\r\n \u003c/Product>\r\n\u003c/OfferCatalog>\r\n\u003cOrder\r\n identifier=\"1\"\r\n orderDate={new Date()}\r\n>\r\n \u003cInvoice identifier=\"2313132\">\r\n \u003cPaymentMethod identifier=\"123243234\" />\r\n \u003c/Invoice>\r\n \u003cProduct\r\n sku=\"SKU 125\"\r\n />\r\n \u003cDeliveryMethod identifier=\"123122222\">\r\n \u003cCountry name=\"New Zealand\" />\r\n \u003c/DeliveryMethod>\r\n\u003c/Order>\r\n```\r\n[GitHub Link](https://github.com/opennetwork/jsx-anything-example/blob/301e408c8c45271519aa139cb4abebb13374223f/src/contents/index.tsx#L15-L48)\r\n\r\nThis can be used to directly create a set of jsx based components without implementation, if these were fully typed, this could be read statically excluding the value of `Date` (while still knowing it was a `Date`)\r\n\r\n```typescript\r\n\u003cscxml>\r\n\r\n \u003cdatamodel>\r\n \u003cdata id=\"eventStamp\"/>\r\n \u003cdata id=\"rectX\" expr=\"0\"/>\r\n \u003cdata id=\"rectY\" expr=\"0\"/>\r\n \u003cdata id=\"dx\"/>\r\n \u003cdata id=\"dy\"/>\r\n \u003c/datamodel>\r\n\r\n \u003cstate id=\"idle\">\r\n \u003ctransition event=\"mousedown\" target=\"dragging\">\r\n \u003cassign location=\"eventStamp\" expr=\"_event.data\"/>\r\n \u003c/transition>\r\n \u003c/state>\r\n\r\n \u003cstate id=\"dragging\">\r\n \u003ctransition event=\"mouseup\" target=\"idle\"/>\r\n \u003ctransition event=\"mousemove\" target=\"dragging\">\r\n \u003cassign location=\"dx\" expr=\"eventStamp.clientX - _event.data.clientX\"/>\r\n \u003cassign location=\"dy\" expr=\"eventStamp.clientY - _event.data.clientY\"/>\r\n \u003cassign location=\"rectX\" expr=\"rectX - dx\"/>\r\n \u003cassign location=\"rectY\" expr=\"rectY - dy\"/>\r\n \u003cassign location=\"eventStamp\" expr=\"_event.data\"/>\r\n \u003c/transition>\r\n \u003c/state>\r\n\r\n\u003c/scxml>\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vscxml/blob/8b6699004c5bceef2e8e3b0335d0fb1279296f2f/src/example.tsx#L5-L32)\r\n\r\nThe above code is a jsx depending on types defined within the same module. \r\n\r\n```typescript\r\nscxml: SCXMLAttributes;\r\ndatamodel: DataModelAttributes;\r\ndata: DataAttributes;\r\nstate: StateAttributes;\r\ntransition: TransitionAttributes;\r\nassign: AssignAttributes;\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vscxml/blob/8b6699004c5bceef2e8e3b0335d0fb1279296f2f/src/jsx.ts#L37-L42)\r\n\r\n\r\nActions can also be defined statically, which if typed, would be readable statically. \r\n\r\n```typescript\r\nexport const Action: ActionNode = createToken(ActionSymbol);\r\n```\r\n[GitHub Link](https://github.com/opennetwork/vview/blob/61bd9e198cb4c056fe71b05d50634d345c8c7cf4/src/index.ts#L42)\r\n\r\n## Related Tickets\r\n\r\nGiven the implementation of https://github.com/microsoft/TypeScript/issues/34319 an implementor will be able to use \r\n\r\n```\r\nfunction h(source, options, ...children) implements CreateNodeFn;\r\n```\r\n\r\nWhich can then be restricted by providing generics. \r\n\r\n","author":{"url":"https://github.com/fabiancook","@type":"Person","name":"fabiancook"},"datePublished":"2021-05-09T22:40:52Z","url":"https://github.com/microsoft/TypeScript/issues/44018"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2cb4000f-2975-12ca-df05-636f2ec7a60f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CF70:4279A:C76233:10D1301:6B0101D3 |
| html-safe-nonce | f770460a90cac34e87bcb22b29a1cc64a81e1d6b0b6e403a794c03032406c93c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjcwOjQyNzlBOkM3NjIzMzoxMEQxMzAxOjZCMDEwMUQzIiwidmlzaXRvcl9pZCI6IjYyMTE2Mjc0NzYxODQwMDcxMjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eeffdd9c3482f327eb2706c9c412e7e9c9e2b3ad4c4f2aa89fcfa7693090789c |
| hovercard-subject-tag | issue:882915101 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/44018/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b6aa464b9579016e12003b98f39ed532889d13a3980e572c2d6de1b8d6ea4e91/microsoft/TypeScript/issues/44018 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b6aa464b9579016e12003b98f39ed532889d13a3980e572c2d6de1b8d6ea4e91/microsoft/TypeScript/issues/44018 |
| og:image:alt | Suggestion 🔍 Search Terms jsx jsxFactory I found related tickets, but this ticket information is very long winded and I believe this requires its own ticket: #21699 #14729 #15217 #29376 ✅ Viability... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fabiancook |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width