From f5dffdf5a568a8c4aaac79ceaef31fe28a8efd1c Mon Sep 17 00:00:00 2001 From: Sine Date: Thu, 6 Feb 2020 18:06:56 -0500 Subject: [PATCH] Added type definitions for shevyjs (#42050) * Added type definitions for shevyjs * Removed example code * Fixed typos --- types/shevyjs/constants/index.d.ts | 7 +++++ types/shevyjs/index.d.ts | 35 +++++++++++++++++++++ types/shevyjs/shevyjs-tests.ts | 50 ++++++++++++++++++++++++++++++ types/shevyjs/tsconfig.json | 23 ++++++++++++++ types/shevyjs/tslint.json | 1 + types/shevyjs/types.d.ts | 28 +++++++++++++++++ types/shevyjs/utils/index.d.ts | 17 ++++++++++ 7 files changed, 161 insertions(+) create mode 100644 types/shevyjs/constants/index.d.ts create mode 100644 types/shevyjs/index.d.ts create mode 100644 types/shevyjs/shevyjs-tests.ts create mode 100644 types/shevyjs/tsconfig.json create mode 100644 types/shevyjs/tslint.json create mode 100644 types/shevyjs/types.d.ts create mode 100644 types/shevyjs/utils/index.d.ts diff --git a/types/shevyjs/constants/index.d.ts b/types/shevyjs/constants/index.d.ts new file mode 100644 index 0000000000..462cff6dde --- /dev/null +++ b/types/shevyjs/constants/index.d.ts @@ -0,0 +1,7 @@ +import { FontScalePresets, Options, Heading } from '../types'; + +export const fontScalePresets: FontScalePresets; + +export const defaultOptions: Options; + +export const headings: Heading[]; diff --git a/types/shevyjs/index.d.ts b/types/shevyjs/index.d.ts new file mode 100644 index 0000000000..4304ef822c --- /dev/null +++ b/types/shevyjs/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for shevyjs 1.1 +// Project: https://github.com/kyleshevlin/shevyjs#readme +// Definitions by: nonAlgebraic +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import { getFontUnit, getFontScale } from './utils'; +import { Options, RhythmProperties, Factor } from './types'; + +export default class Shevy { + constructor(options?: Partial); + + baseFontSize: Options['baseFontSize']; + baseFontUnit: ReturnType; + baseLineHeight: Options['baseLineHeight']; + baseFontScale: ReturnType; + addMarginBottom: Options['addMarginBottom']; + proximity: Options['proximity']; + proximityFactor: Options['proximityFactor']; + + h1: RhythmProperties; + h2: RhythmProperties; + h3: RhythmProperties; + h4: RhythmProperties; + h5: RhythmProperties; + h6: RhythmProperties; + + body: Pick>; + + content: RhythmProperties; + + lineHeightSpacing(factor?: Factor): string; + + baseSpacing(factor?: Factor): string; +} diff --git a/types/shevyjs/shevyjs-tests.ts b/types/shevyjs/shevyjs-tests.ts new file mode 100644 index 0000000000..ce3167bd8e --- /dev/null +++ b/types/shevyjs/shevyjs-tests.ts @@ -0,0 +1,50 @@ +import Shevy from 'shevyjs'; +import { Options } from 'shevyjs/types'; +import { headings } from 'shevyjs/constants'; +import * as utils from 'shevyjs/utils'; + +const options: Options = { + baseFontSize: '16px', + baseLineHeight: 1, + baseFontScale: 'majorSecond', + addMarginBottom: true, + proximity: true, + proximityFactor: 1, +}; + +const shevy = new Shevy(options); + +shevy.baseFontSize; // $ExpectType string +shevy.baseFontUnit; // $ExpectType string +shevy.baseLineHeight; // $ExpectType number +shevy.baseFontScale; // $ExpectType number[] +shevy.addMarginBottom; // $ExpectType boolean +shevy.proximity; // $ExpectType boolean +shevy.proximityFactor; // $ExpectType number + +headings.forEach(heading => { + shevy[heading].fontSize; // $ExpectType string + shevy[heading].lineHeight; // $ExpectType number + shevy[heading].marginBottom; // $ExpectType string +}); + +shevy.body.fontSize; // $ExpectType string +shevy.body.lineHeight; // $ExpectType number + +shevy.content.fontSize; // $ExpectType string +shevy.content.lineHeight; // $ExpectType number +shevy.content.marginBottom; // $ExpectType string + +shevy.lineHeightSpacing(1); // $ExpectType string + +shevy.baseSpacing(1); // $ExpectType string + +utils.calcHeadingFontSize(shevy, 1); // $ExpectType string +utils.calcHeadingLineHeight(shevy, 1); // $ExpectType number +utils.calcHeadingMarginBottom(shevy, 1); // $ExpectType string | undefined +utils.calcHeadingMarginBottom(shevy, 1, true); // $ExpectType string | undefined +utils.getFontScale([1, 2, 3]); // $ExpectType number[] +utils.getFontScale('majorSecond'); // $ExpectType number[] +utils.getFontUnit('16px'); // $ExpectType string +utils.getFontValue('16px'); // $ExpectType number +utils.trimArrayToMaxOf6([1, 2, 3, 4, 5, 6, 7, 8]); // $ExpectType number[] diff --git a/types/shevyjs/tsconfig.json b/types/shevyjs/tsconfig.json new file mode 100644 index 0000000000..61d3bb8b8d --- /dev/null +++ b/types/shevyjs/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "shevyjs-tests.ts" + ] +} diff --git a/types/shevyjs/tslint.json b/types/shevyjs/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/shevyjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/shevyjs/types.d.ts b/types/shevyjs/types.d.ts new file mode 100644 index 0000000000..bbfd5fe7e2 --- /dev/null +++ b/types/shevyjs/types.d.ts @@ -0,0 +1,28 @@ +export type Factor = number; + +export type Scale = Factor[]; + +export interface FontScalePresets { + majorSecond: Scale; + minorThird: Scale; + majorThird: Scale; + perfectFourth: Scale; + augmentedFourth: Scale; +} + +export interface Options { + baseFontSize: string; + baseLineHeight: number; + baseFontScale: Scale | keyof FontScalePresets; + addMarginBottom: boolean; + proximity: boolean; + proximityFactor: Factor; +} + +export type Heading = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; + +export interface RhythmProperties { + fontSize: string; + lineHeight: number; + marginBottom: string; +} diff --git a/types/shevyjs/utils/index.d.ts b/types/shevyjs/utils/index.d.ts new file mode 100644 index 0000000000..cea1350421 --- /dev/null +++ b/types/shevyjs/utils/index.d.ts @@ -0,0 +1,17 @@ +import { fontScalePresets } from '../constants'; +import { Scale, Factor } from '../types'; +import Shevy from '../index'; + +export function getFontValue(size: string): number; + +export function getFontUnit(size: string): string; + +export function trimArrayToMaxOf6(array: ReadonlyArray): T[]; + +export function getFontScale(fontScale: Scale | keyof typeof fontScalePresets): number[]; + +export function calcHeadingFontSize(thisArg: Shevy, factor: Factor): string; + +export function calcHeadingLineHeight(thisArg: Shevy, factor: Factor): number; + +export function calcHeadingMarginBottom(thisArg: Shevy, factor: Factor, addMarginBottom?: boolean): string | undefined;