typed-jsx
This package provides utilities for strongly-typed, custom JSX implementations.
Install
# With yarn yarn add typed-jsx # With NPM npm install typed-jsx
Usage
For constructing XML-like hierarchies of elements
This package exposes utility functions and types for working with strongly-typed JSX.
The package exposes the JSX factory function jsx
which simply converts your JSX into a traversable hierarchy of elements.
All JSX elements are instances of the class Element
, which you can test for using the isJSXElement
helper.
Class components must extend Component
, which is simply an empty base class for tagging the objects.
You can check if a component is a class component using the isClassComponent
and whether an object is an instance of a class component using the isComponentInstance
helpers.
You can also define factory components, which return Component instances (but this is currently causing errors with the TypeScript compiler, despite being allowed in the spec) .
Finally, the visit
function provides a utility for traversing the JSX elements.
/* @jsx jsx */ ; // Function component ; console.log;// Will print:// Element { element: [Function: MyFunctionComponent], props: { n: 1 } } // Class component console.log;// Will print:// Element { element: [Function: MyClassComponent], props: { s: 'abc' } } // Factory component ; console.log;// Will print:// Element { element: [Function: MyFactoryComponent], props: { b: true } } // isClassComponent console.log'isClassComponent(MyFunctionComponent) =', isClassComponentMyFunctionComponent;// Prints isClassComponent(MyFunctionComponent) = falseconsole.log'isClassComponent(MyClassComponent) =', isClassComponentMyClassComponent;// Prints isClassComponent(MyClassComponent) = trueconsole.log'isClassComponent(MyFactoryComponent) =', isClassComponentMyFactoryComponent;// Prints isClassComponent(MyFactoryComponent) = false // isComponentInstance console.log'isComponentInstance(MyFunctionComponent({ n: 1 })) =', isComponentInstanceMyFunctionComponent;// Prints isComponentInstance(MyFunctionComponent({ n: 1 })) = falseconsole.log 'isComponentInstance(new MyClassComponent({ s: "abc" })) =', isComponentInstancenew MyClassComponent,;// Prints isComponentInstance(new MyClassComponent({ s: "abc" })) = trueconsole.log 'isComponentInstance(MyFactoryComponent({ b: true })) =', isComponentInstanceMyFactoryComponent,;// Prints isComponentInstance(MyFactoryComponent({ b: true })) = true // visit visit ,console.logelement, parent,;// This will traverse the JSX and print each of the elements in a depth-first search
For constructing hierarchical data structures
The package also exposes another JSX factory function data
, which immediately invokes the "component" functions, class constructors and factory functions to create a nested hierarchy of objects, which allows for a terse and natural way of describing hierarchical data structures, for example file hierarchies.
/* @jsx data */ ; ; ; ; ; console.logJSON.stringifyfiles, undefined, 2;// Will print://// {// "type": "directory",// "name": "root",// "children": [// {// "type": "directory",// "name": "sub",// "children": [// {// "type": "file",// "name": "file1",// "data": "..."// }// ]// },// {// "type": "directory",// "name": "sub2",// "children": [// {// "type": "file",// "name": "file2",// "data": "..."// },// {// "type": "symlink",// "name": "file3",// "target": "../sub/file1"// }// ]// }// ]// }
Type reference
The package also provides a number of helper types for working with JSX. The following is a reference of all exported types:
; /** Base class of JSX elements */declare /** Base interface of JSX Elements */ /** The map of supported intrinsic elements and their attributes */ /** An intrinsic JSX element */ /** A function component */declare ; /** A JSX function element */ /** Base class of all class components */declare /** The instance type of class components and return type of factory components */ /** A class component */declare ; /** A JSX class element */ /** A factory component */declare ; /** A JSX factory element */ /** The sum type of all component types */declare ; /** Sum type of intrinsic and component JSX elements */declare ; /** Returns the type of the props argument for T */declare ; /** Returns the return type `R` of a component */declare ; /** Returns the array representation of children of the props P */declare ; /** Function component */declare ;/** Class component */declare ;/** Factory component */declare ;declare /** An error indicating an invalid JSX element */declare /** Returns true if a JSX component is a class (must inherit from Component) */declare ; /** Returns true if an object is a ClassElement */declare ; ; /** Returns true if `child` is a sub-class of `parent` */declare ;/** Intrinsic component */declare ;/** Function component */declare ;/** Class component */declare ;/** Factory component */declare ;declare ;;declare ;;