TypeScript
LyteNyte Grid offers first-class TypeScript support. This guide outlines best practices for extending LyteNyte Grid's column and API interfaces and avoiding errors through proper type checking.
Main Grid Types
There are five primary types to know about when working with LyteNyte Grid:
Grid.GridSpec: A generic type constraint used by other LyteNyte Grid interfaces.Grid.Props: The interface that describes the props that the<Grid />component accepts.Grid.Column: The base interface for a column in LyteNyte Grid.Grid.API: The base interface that describes the LyteNyte Grid API.RowSource: The base interface for a row data source that you can provide to the grid. All row data sources extend this base type.
You do not need to memorize these types. Understanding how these types fit together will help you get the most out of TypeScript and LyteNyte Grid.
Grid Specification
Grid.GridSpec is one of the five primary grid types that developers typically define.
It’s a type constraint used by other grid types, and LyteNyte Grid never reads it at runtime.
Grid specification has four optional type parameters and four optional properties. Each property extends the grid’s type in a specific way. The base definition is as follows:
export interface GridSpec< Data = unknown, ColExt extends Record<string, any> = object, Source extends RowSource<Data> = RowSource, Ext extends Record<string, any> = object,> { readonly data?: Data; readonly column?: ColExt; readonly source?: Source; readonly api?: Ext;}Specification Parameters and Properties
The four optional properties for Grid.GridSpec include:
-
Data Property: LyteNyte Grid interfaces use this property to infer the structure of the row data items provided to the grid.
-
Column Property: An extension of the base column definition provided by LyteNyte Grid. Use this property to add additional column properties that are specific to your application.
-
Source Property: The row data source used by the grid. LyteNyte Grid uses this property to provide type completions for API methods that are specific to each row source.
-
API Property: An extension of the base grid API that adds custom properties and methods specific to your application.
The four optional type parameters for Grid.GridSpec include:
-
DataType: Describes the shape of the row data that the grid receives. For example, if the row data is anumber[], define the grid spec like this:interface GridSpec {readonly data: number[];} -
ColExtType: Describes additional properties you want to add to columns. LyteNyte Grid combines these additional properties with the baseGrid.Columntype. For example, the code below shows two equivalent definitions:interface GridSpec {readonly column: { readonly sort?: "asc" | "desc" | null };}type ColumnWithSort = Grid.Column<GridSpec>;interface ColumnWithSort extends Grid.Column {readonly sort?: "asc" | "desc" | null;}Info
The
GridSpecapproach is recommended because it keeps column extensions and row data typing in one place. -
SourceType: Describes the row data source you provide. Each row data source shares common capabilities, but each source type also adds source-specific capabilities and functions.The
sourceproperty exposes those capabilities through the grid types. The code below shows an example using theRowSourceServertype from the server row data source:interface GridSpec {readonly source: RowSourceServer;} -
ExtType: Describes the API extensions that you provide through theapiExtensionproperty on the grid. UseExtto add user-defined functions and state to the grid’s API. When you typeExton the grid spec, you extend the API type.LyteNyte Grid also extends the API type with the provided
Sourcetype, so the final API type is the combination:Grid.API & Source & Ext.The code below shows an example that adds a
notifyUsermethod to LyteNyte Grid’s API:interface GridSpec {readonly api: { notifyUser: (msg: string) => void };}
Combining Specification Types and Properties
You can combine all properties to create a full set of specified grid types.
In the example below, each part of the GridSpec is defined and used with
the main types used by LyteNyte Grid:
interface GridSpec { readonly data: number[]; readonly column: { readonly sort?: "asc" | "desc" | null }; readonly source: RowSourceServer; readonly api: { notifyUser: (msg: string) => void };}
type Props = Grid.Props<GridSpec>;type Column = Grid.Column<GridSpec>;type API = Grid.API<GridSpec>;After defining these types, you can derive property-specific types. For example, define renderer types as follows:
type CellRenderer = Required<Grid.Column<GridSpec>>["cellRenderer"];type HeaderRenderer = Required<Grid.Column<GridSpec>>["headerRenderer"];Namespace and Component
The Grid export in LyteNyte Grid is both the root component and a type namespace.
The namespace contains all the types used by LyteNyte Grid’s publicly exposed functions
and components. This means the following code is valid:
function GridWrapper<K extends Grid.GridSpec>(props: Grid.Props<K>) { return <Grid {...props} />;}This guide does not cover the details of why this works in
TypeScript, but you do not need to actively think about it.
TypeScript does not allow Grid itself to be used as a type,
so TypeScript prevents misuse in places where a type is
expected. For example, this code fails type checking:
type MyGrid = Grid;Component and T Namespaces
The Grid namespace has two sub-namespaces for types that are less frequently used:
Grid.T: Defines auxiliary types used by the various functions and renderers of LyteNyte Grid, for exampleGrid.T.HeaderParams.Grid.Component: Defines the props for the various headless component parts that make up the grid.
The Grid.T namespace is most useful when defining custom components. For example, the following
two cell renderer definitions are equivalent. The method you choose is developer preference.
interface Spec { readonly data: number[];}
export const MyCell: Grid.Column<Spec>["Cell"] = (props) => { // my component definition};
export function MyCell2(props: Grid.T.CellRendererParams<Spec>) { // my component definition}Next Steps
- Getting Started: Get started with LyteNyte Grid, a modern React data grid designed for enterprise-scale data challenges.
- Grid Reactivity: Learn how LyteNyte Grid enables declarative reactivity.
- API Extensions: Extend LyteNyte Grid's base API with custom functions and state.
