Skip to content

Commit

Permalink
feat: add TypeScript definitions (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey authored and gregberge committed May 13, 2018
1 parent 9ea86f3 commit db19796
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 9 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"repository": "[email protected]:smooth-code/loadable-components.git",
"main": "dist/loadable-components.cjs.js",
"jsnext:main": "dist/loadable-components.es.js",
"types": "types/index.d.ts",
"module": "dist/loadable-components.es.js",
"author": "Greg Bergé <[email protected]>",
"keywords": [
Expand All @@ -24,13 +25,15 @@
"ci": "yarn build && yarn lint && bundlesize && yarn test --coverage && codecov",
"format": "prettier --write \"src/**/*.js\" \"*.md\"",
"lint": "eslint .",
"typecheck:ts": "dtslint types",
"release": "yarn build && standard-version && conventional-github-releaser --preset angular",
"test": "jest"
},
"peerDependencies": {
"react": "^15.0.0 || ^16.0.0"
},
"devDependencies": {
"@types/react": "^16.3.14",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
Expand All @@ -44,6 +47,7 @@
"bundlesize": "^0.17.0",
"codecov": "^3.0.1",
"conventional-github-releaser": "^2.0.2",
"dtslint": "^0.3.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.19.1",
Expand Down
3 changes: 3 additions & 0 deletions types/TestComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react';

export default class Test extends React.Component<{prop1: string, prop2: number}> {}
56 changes: 56 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// TypeScript Version: 2.4
// export as namespace LoadableComponents;

declare module 'loadable-components' {
import * as React from 'react';

interface DefaultImportedComponent<P> {
default: React.ComponentType<P>;
}

type DefaultComponent<P> = React.ComponentType<P> | DefaultImportedComponent<P>;

export interface ComponentTracker {
track: (component: React.Component, modules: any, index?: number) => number;

get: (id: number) => React.ComponentType;
getAll: () => { [key: number]: React.ComponentType };
reset: () => void;
}

export interface LoadableOptions<T> {
ErrorComponent?: React.ComponentType;
LoadingComponent?: React.ComponentType;
render?: (options: { loading: boolean, error: boolean, ownProps: T, Component: React.ComponentType<T> }) => React.ReactElement<T>;
modules?: any;
}

interface Loadable<T> extends React.ComponentClass<T> {
Component: React.ComponentClass;
loadingPromise: Promise<any>;
load(): Promise<any>;
}

export const componentTracker: ComponentTracker;
export const LOADABLE: string;

export function loadComponents(): Promise<any>;

export function getState(): { __LOADABLE_STATE__: { children: Array<{ id: number }> } };

export default function loadable<T>(
getComponent: () => Promise<DefaultComponent<T>>,
options?: LoadableOptions<T>
): Loadable<T>;
}

declare module 'loadable-components/server' {
import * as React from 'react';

export function getLoadableState(
rootElement: React.ReactElement<{}>,
rootContext?: any,
fetchRoot?: boolean,
tree?: any,
): Promise<any>;
}
27 changes: 27 additions & 0 deletions types/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* tslint-disable */
// TypeScript Version: 2.7

import * as React from 'react';
import loadableComponent, { loadComponents } from 'loadable-components';
import { getLoadableState } from 'loadable-components/server';

const App = loadableComponent(() => import('./TestComponent'));

App.load();

const app = (
<App prop1={"1"} prop2={2}/>
);

getLoadableState(app).then(() => {
// Load all components needed before starting rendering
loadComponents().then(() => {
// $ExpectError
const a1 = <App prop1={4} prop2="4"/>;

// $ExpectError
const a2 = <App/>;

const a3 = <App prop1={"4"} prop2={4}/>;
});
});
12 changes: 12 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es5", "es2015", "dom"],
"jsx": "react",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noEmit": true,
"strictFunctionTypes": false
}
}
8 changes: 8 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"semicolon": [true, "always"],
"indent": [
true,
"tabs"
]
}
Loading

0 comments on commit db19796

Please sign in to comment.