[@types/styletron-engine-atomic] foundation for styletron-engine-atomic (#35727)

* [@types/styletron-engine-atomic] foundation for styletron-engine-atomic typings

* fix: add TypeScript version

* fix: linting + add cache types
This commit is contained in:
jhey tompkins 2019-05-28 22:24:17 +01:00 committed by Sheetal Nandi
parent 6c261bd1d7
commit 4e081f6fc7
4 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,85 @@
// Type definitions for styletron-engine-atomic 1.1
// Project: https://github.com/styletron/styletron
// Definitions by: Jhey Tompkins <https://github.com/jh3y>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.9
import { KeyframesObject, FontFace as FontFaceObject, StandardEngine, StyleObject } from 'styletron-standard';
export class SequentialIDGenerator {
prefix: string;
count: number;
offset: number;
msb: number;
power: number;
constructor(prefix: string);
next(): string;
increment(): number;
}
export class Cache<T> {
cache: {[key: string]: string};
idGenerator: SequentialIDGenerator;
key: string;
onNewValue: (cache: Cache<T>, id: string, value: any) => any;
constructor(
idGenerator: SequentialIDGenerator,
onNewValue: (cache: Cache<T>, id: string, value: any) => any,
);
addValue(key: string, value: T): number;
}
export class MultiCache<T> {
caches: {[key: string]: Cache<T>};
idGenerator: SequentialIDGenerator;
onNewCache: (key: string, cache: Cache<T>, insertAtIndex: number) => any;
onNewValue: (cache: Cache<T>, id: string, value: T) => any;
sortedCacheKeys: string[];
constructor(
idGenerator: SequentialIDGenerator,
onNewCache: () => any,
onNewValue: () => any,
);
getCache(key: string): Cache<T>;
getSortedCacheKeys(): string[];
}
export type hydrateType = HTMLCollectionOf<HTMLStyleElement> | HTMLStyleElement[] | NodeListOf<HTMLStyleElement>;
export interface Sheet {
css: string;
attrs: { [key: string]: string };
}
export interface ClientOptions {
hydrate?: hydrateType;
container?: Element;
prefix?: string;
}
export interface ServerOptions {
prefix?: string;
}
export class Client implements StandardEngine {
constructor(opts?: ClientOptions);
styleElements: { [key: string]: HTMLStyleElement };
fontFaceSheet: HTMLStyleElement;
keyframesSheet: HTMLStyleElement;
styleCache: MultiCache<{pseudo: string, block: string}>;
keyframesCache: Cache<KeyframesObject>;
fontFaceCache: Cache<FontFaceObject>;
renderStyle(style: StyleObject): string;
renderKeyframes(keyframes: KeyframesObject): string;
renderFontFace(fontFace: FontFaceObject): string;
}
export class Server implements StandardEngine {
constructor(opts?: ServerOptions)
styleRules: { [key: string]: string };
keyframesRules: string;
fontFaceRules: string;
getStylesheets(): Sheet[];
getStylesheetsHtml(className?: string): string;
getCss(): string;
renderStyle(style: StyleObject): string;
renderKeyframes(keyframes: KeyframesObject): string;
renderFontFace(fontFace: FontFaceObject): string;
}

View File

@ -0,0 +1,20 @@
import {
Client,
Server
} from 'styletron-engine-atomic';
const validOptions = {
prefix: 'test-prefix__'
};
const invalidOptions = {
hydrate: 'erroneous hydration'
};
new Client(validOptions);
new Client(invalidOptions); // $ExpectError
new Server({prefix: 1234}); // $ExpectError
const myServer = new Server(validOptions);
myServer.getCss(); // $ExpectType string
myServer.getStylesheetsHtml(); // $ExpectType string

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"styletron-engine-atomic-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }