diff --git a/types/storybook__addons/index.d.ts b/types/storybook__addons/index.d.ts new file mode 100644 index 0000000000..5db0e048a1 --- /dev/null +++ b/types/storybook__addons/index.d.ts @@ -0,0 +1,85 @@ +// Type definitions for @storybook/addons 4.1 +// Project: https://github.com/storybooks/storybook/tree/master/lib/addons +// Definitions by: Bob Matcuk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.1 + +import Channel = require("@storybook/channels"); +import { RenderFunction, StoryDecorator } from "@storybook/react"; + +export const mockChannel: Channel; + +export interface Context { + kind: string; + story: string; +} + +export type GetStoryFunc = (context: Context) => ReturnType; + +export interface Wrapper { + (getStory: GetStoryFunc, context: Context, optsAndParams: { options: Options, parameters: Parameters }): + ReturnType; +} + +export interface MakeDecoratorOptions { + name: string; + parameterName: ParameterName; + wrapper: Wrapper; + skipIfNoParametersOrOptions?: boolean; + allowDeprecatedUsage?: boolean; +} + +export interface DecoratorContext extends Context { + parameters?: Record; +} + +export interface DecoratorWithOptions { + // Support for older, but not deprecated style: + // .addDecorator(decorator(options)) + (story: RenderFunction, context: DecoratorContext): ReturnType; + + // Support for the deprecated style: + // .add(decorator(options)(() => ) + (story: RenderFunction): (context: DecoratorContext) => ReturnType; +} + +export interface Decorator { + // Supports newer style: + // .addDecorator(decorator) + // .add('story', () => ) + (story: RenderFunction, context: DecoratorContext): ReturnType; + + // Support for older styles which pass options. + // See DecoratorWithOptions above. + (options: Options): DecoratorWithOptions; + (...options: Options[]): DecoratorWithOptions; +} + +// If you manually specify the type parameters, the first type (ParameterName) +// must match the "parameterName" option, ie: +// +// makeDecorator<"myParam", ..., ...>({ parameterName: "myParam", ... }) +// +// TypeScript is pretty good about inferring all of the type generics if you +// include the type declarations on the options/parameters in your wrapper +// function. +export function makeDecorator( + options: MakeDecoratorOptions): + Decorator; + +export class AddonStore { + constructor(); + getChannel(): Channel; + hasChannel(): boolean; + setChannel(channel: any): void; + getPreview(): any; + getDatabase(): any; + setDatabase(database: any): void; + getPanels(): any; + addPanel(name: any, panel: any): void; + register(name: any, loader: any): any; + loadAddons(api: any): any; +} + +declare const DefaultAddonStore: AddonStore; +export default DefaultAddonStore; diff --git a/types/storybook__addons/storybook__addons-tests.ts b/types/storybook__addons/storybook__addons-tests.ts new file mode 100644 index 0000000000..14e1227398 --- /dev/null +++ b/types/storybook__addons/storybook__addons-tests.ts @@ -0,0 +1,25 @@ +import addons, { makeDecorator } from "@storybook/addons"; +import { addDecorator, storiesOf } from "@storybook/react"; + +// Example from Storybook documentation +const withNotes = makeDecorator({ + name: "withNotes", + parameterName: "notes", + wrapper: (getStory, context, {options, parameters}: {options: number, parameters: string}) => { + const channel = addons.getChannel(); + channel.emit("MYADDON/add_notes", parameters); + return getStory(context); + } +}); + +// new style +addDecorator(withNotes); + +// old style, but still supported +addDecorator(withNotes(42)); + +// the deprecated style actually isn't supported by +// @storybook/react's typings, so, no test for that... +storiesOf("Hello", module) + .addDecorator(withNotes) // new style + .addDecorator(withNotes(42)); // old style, but still supported diff --git a/types/storybook__addons/tsconfig.json b/types/storybook__addons/tsconfig.json new file mode 100644 index 0000000000..7dfe3ed67f --- /dev/null +++ b/types/storybook__addons/tsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "paths": { + "@storybook/addons": [ + "storybook__addons" + ], + "@storybook/channels": [ + "storybook__channels" + ], + "@storybook/react": [ + "storybook__react" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "storybook__addons-tests.ts" + ] +} diff --git a/types/storybook__addons/tslint.json b/types/storybook__addons/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/storybook__addons/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/storybook__channels/index.d.ts b/types/storybook__channels/index.d.ts new file mode 100644 index 0000000000..f9e43d156e --- /dev/null +++ b/types/storybook__channels/index.d.ts @@ -0,0 +1,39 @@ +// Type definitions for @storybook/channels 4.1 +// Project: https://github.com/storybooks/storybook/tree/master/lib/channels +// Definitions by: Bob Matcuk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.1 + +type EventName = PropertyKey; +type EventHandler = (event: Channel.Event) => void; +type Listener = (...args: any) => void; + +declare namespace Channel { + interface Event { + type: EventName; + args: any; + from: string; + } + + interface Transport { + setHandler(handler: EventHandler): void; + send(event: any): void; + } +} + +declare class Channel { + constructor(options?: { transport?: Channel.Transport, async?: boolean }); + addListener(type: EventName, listener: Listener): void; + addPeerListener(type: EventName, listener: Listener): void; + emit(type: EventName, ...args: any): void; + eventNames(): EventName[]; + listenerCount(type: EventName): number; + listeners(type: EventName): Listener[]; + on(type: EventName, listener: Listener): void; + once(type: EventName, listener: Listener): void; + prependListener(type: EventName, listener: Listener): void; + removeAllListeners(type: EventName): void; + removeListener(type: EventName, listener: Listener): void; +} + +export = Channel; diff --git a/types/storybook__channels/storybook__channels-tests.ts b/types/storybook__channels/storybook__channels-tests.ts new file mode 100644 index 0000000000..0f2d8c39e9 --- /dev/null +++ b/types/storybook__channels/storybook__channels-tests.ts @@ -0,0 +1,21 @@ +import Channel = require("@storybook/channels"); + +const transport: Channel.Transport = { + setHandler(handler) { }, + send(event) { }, +}; +const channel = new Channel({ transport, async: true }); +const listener = (...args: any) => { }; +channel.addListener("event", listener); +channel.addPeerListener("event", listener); +channel.emit("event", 42); +channel.on("event", listener); +channel.once("event", listener); +channel.prependListener("event", listener); + +const events: PropertyKey[] = channel.eventNames(); +const count: number = channel.listenerCount("event"); +const listeners: Array<(...args: any) => void> = channel.listeners("event"); + +channel.removeListener("event", listener); +channel.removeAllListeners("event"); diff --git a/types/storybook__channels/tsconfig.json b/types/storybook__channels/tsconfig.json new file mode 100644 index 0000000000..4c49227623 --- /dev/null +++ b/types/storybook__channels/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "paths": { + "@storybook/channels": [ + "storybook__channels" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "storybook__channels-tests.ts" + ] +} diff --git a/types/storybook__channels/tslint.json b/types/storybook__channels/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/storybook__channels/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }