From 01817f6e56c5cc73b7f7ca0329841ea0245b1b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Droz=CC=87dz=CC=87yn=CC=81ski?= Date: Mon, 14 Oct 2024 10:07:12 +0200 Subject: [PATCH] Change `strict` to `true` in `tsconfig.json` --- .changeset/slimy-taxis-rhyme.md | 5 +++++ packages/core/src/constructor.test.ts | 4 ++-- packages/core/src/constructor.ts | 22 ++++++++++---------- packages/core/src/css.test.ts | 2 +- packages/core/src/css.ts | 8 ++++---- packages/core/src/pattern.test.ts | 4 ++-- packages/core/src/pattern.ts | 29 +++++++++++++++++---------- packages/core/src/sheet/index.test.ts | 2 +- packages/core/src/sheet/tag.ts | 4 ++-- packages/core/src/tags.ts | 4 ++-- packages/core/tsconfig.json | 2 +- 11 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 .changeset/slimy-taxis-rhyme.md diff --git a/.changeset/slimy-taxis-rhyme.md b/.changeset/slimy-taxis-rhyme.md new file mode 100644 index 0000000..c08a15a --- /dev/null +++ b/.changeset/slimy-taxis-rhyme.md @@ -0,0 +1,5 @@ +--- +"@teiler/core": patch +--- + +Change `strict` to `true` in `tsconfig.json` diff --git a/packages/core/src/constructor.test.ts b/packages/core/src/constructor.test.ts index 9ba9455..1007ed5 100644 --- a/packages/core/src/constructor.test.ts +++ b/packages/core/src/constructor.test.ts @@ -206,7 +206,7 @@ describe('insert', () => { hydrate: jest.fn<(ids: string[]) => void>(), } - const definition: StyleDefinition<'div', {}> = { + const definition: StyleDefinition = { id: 'teiler-1ep7axc', styles: [[['from { background-color: red; } to { background-color: green; }'], []]], tag: null, @@ -229,7 +229,7 @@ describe('insert', () => { hydrate: jest.fn<(ids: string[]) => void>(), } - const definition: StyleDefinition<'div', {}> = { + const definition: StyleDefinition = { id: 'tytz3vv', styles: [[['body { color: red; }'], []]], tag: null, diff --git a/packages/core/src/constructor.ts b/packages/core/src/constructor.ts index 98657a8..0dcbf99 100644 --- a/packages/core/src/constructor.ts +++ b/packages/core/src/constructor.ts @@ -11,17 +11,17 @@ type Arguments = { theme: DefaultTheme } & Props -type CSS = { styles: Style[]; id: string; __css__: true } -type Expression = (props: Arguments) => string | boolean | CSS +type CSS = { styles: Style[]; id: string; __css__: true } +type Expression = (props: Arguments) => string | boolean | CSS type Raw = string | number type Properties = Expression | StyleDefinition | Pattern | TeilerComponent | Raw type Style = [string[], Properties[]] -type StyleDefinition = { +type StyleDefinition = { type: 'component' | 'global' | 'keyframes' id: string styles: Array> - tag: Target | null + tag: Target } type TeilerComponent = { @@ -37,7 +37,7 @@ function styled>( createComponent: CreateCallback, stringOrBinded: TeilerComponent | ReadonlyArray, ...properties: Properties[] -): ExtendCallback | Type { +): Type | ExtendCallback { if (Array.isArray(stringOrBinded)) { const strings = stringOrBinded as ReadonlyArray const style: Style = [Array.from(strings), properties] @@ -45,7 +45,7 @@ function styled>( return createComponent(styleDefinition) } else { const binded = stringOrBinded as TeilerComponent - return (strings: ReadonlyArray, ...properties: Expression[]) => { + return (strings: ReadonlyArray, ...properties: Properties[]) => { const style: Style = [Array.from(strings), properties] const styleDefinition = compiler(binded.styleDefinition.tag, [...binded.styleDefinition.styles, style]) return createComponent(styleDefinition) @@ -53,7 +53,7 @@ function styled>( } } -type Compiler = (tag: Target, styles: Array>) => StyleDefinition +type Compiler = (tag: Target, styles: Array>) => StyleDefinition const component: Compiler = (tag: Target, styles: Array>): StyleDefinition => { const id = styles.reduce((acc, [strings]) => acc + strings.join(''), '') @@ -66,7 +66,7 @@ const component: Compiler = (tag: Target, st } } -const global: Compiler = (tag: null, styles: Array>): StyleDefinition => { +const global: Compiler = (tag: Target, styles: Array>): StyleDefinition => { const id = styles.reduce((acc, [strings]) => acc + strings.join(''), '') return { @@ -89,14 +89,14 @@ function keyframes(strings: ReadonlyArray, ...properties: Raw[]): StyleD } } -function css(strings: ReadonlyArray, ...properties: Exclude, Expression>[]): CSS { +function css(strings: ReadonlyArray, ...properties: Exclude, Expression>[]): CSS { const style: Style = [Array.from(strings), properties] const styles = [style] const id = styles.reduce((acc, [strings]) => acc + strings.join(''), '') return { styles: styles, id: 't' + hash(id), __css__: true } } -function insert(sheet: Sheet, definition: StyleDefinition, props: Arguments): string | null { +function insert(sheet: Sheet, definition: StyleDefinition, props: Arguments): string | null { const { styles, type } = definition const { css, definitions } = compile(styles, props) const compiledId = hash(css) @@ -120,5 +120,5 @@ function insert(sheet: Sheet, definition: StyleDefinition return null } -export type { Arguments, Compiler, CreateCallback, CSS, DefaultTheme, Properties, Sheet, Style, StyleDefinition, TeilerComponent, HTMLElements } +export type { Arguments, Compiler, CreateCallback, CSS, DefaultTheme, Properties, Raw, Sheet, Style, StyleDefinition, TeilerComponent, HTMLElements } export { component, css, global, insert, keyframes, styled } diff --git a/packages/core/src/css.test.ts b/packages/core/src/css.test.ts index 2e62a8c..45f11c8 100644 --- a/packages/core/src/css.test.ts +++ b/packages/core/src/css.test.ts @@ -127,7 +127,7 @@ describe('compile', () => { }) test('with css', () => { - const css: CSS = { + const css: CSS<{}> = { id: 't1vxhd59', styles: [[['background: ', ';'], ['red']]], __css__: true, diff --git a/packages/core/src/css.ts b/packages/core/src/css.ts index bc213d1..70db723 100644 --- a/packages/core/src/css.ts +++ b/packages/core/src/css.ts @@ -1,4 +1,4 @@ -import type { Arguments, HTMLElements } from './constructor' +import type { Arguments, HTMLElements, Raw } from './constructor' import type { Pattern, Style, StyleDefinition } from '.' import { middleware, prefixer, rulesheet, serialize, stringify, compile as stylisCompile } from 'stylis' @@ -11,10 +11,10 @@ type CompileResult = { } function compile(styles: Array>, props: Arguments): CompileResult { - return styles.reduce( + return styles.reduce>( (result, [strings, properties]) => { const compiled = strings - .reduce((acc, strings, index) => { + .reduce<(string | true | Raw)[]>((acc, strings, index) => { acc = [...acc, strings] const property = properties.at(index) @@ -67,7 +67,7 @@ function compile(styles: Array>, props: Arguments): C } function transpile(css: string): string { - const results = [] + const results: string[] = [] serialize( stylisCompile(css), diff --git a/packages/core/src/pattern.test.ts b/packages/core/src/pattern.test.ts index 8a4538d..a7d43c9 100644 --- a/packages/core/src/pattern.test.ts +++ b/packages/core/src/pattern.test.ts @@ -57,13 +57,13 @@ describe('pattern', () => { }) }) -type TestComponent = TeilerComponent & { +type TestComponent = TeilerComponent & { render(): string } const sheet = createStyleSheet({}) -const createComponent = (styles: StyleDefinition): TestComponent => { +const createComponent = (styles: StyleDefinition): TestComponent => { return { styleDefinition: styles, render() { diff --git a/packages/core/src/pattern.ts b/packages/core/src/pattern.ts index d5dc53f..b383685 100644 --- a/packages/core/src/pattern.ts +++ b/packages/core/src/pattern.ts @@ -6,29 +6,29 @@ import hash from './hash' type PropertiesWithPattern = Properties | Pattern -type Pattern = { +type Pattern = { styles: Style[] tag: Target id: string __pattern__: true } -type ExtendCallback = (string: ReadonlyArray, ...properties: Properties>[]) => Pattern> +type ExtendCallback = (string: ReadonlyArray, ...properties: Properties>[]) => Pattern> -type Constructor = { - (pattern: Pattern): ExtendCallback - (string: ReadonlyArray, ...properties: PropertiesWithPattern[]): Pattern +type Constructor = { + (pattern: Pattern): ExtendCallback + (string: ReadonlyArray, ...properties: PropertiesWithPattern[]): Pattern } -type ConstructorWithTags = Constructor<'div'> & { [K in HTMLElements]: Constructor } & { global: Constructor } - type Infer = Component extends Pattern ? P & Props : Props -const construct = (tag: HTMLElements) => { - return (stringOrPattern: Pattern | ReadonlyArray, ...properties: Properties[]): Pattern | ExtendCallback => { +const construct = (tag: Target) => { + function create(stringOrPattern: Pattern): ExtendCallback + function create(stringOrPattern: ReadonlyArray, ...properties: PropertiesWithPattern[]): Pattern + function create(stringOrPattern: Pattern | ReadonlyArray, ...properties: Properties[]): Pattern | ExtendCallback { if ('__pattern__' in stringOrPattern) { return (strings: ReadonlyArray, ...properties: Properties>[]) => { - const style: Style = [Array.from(strings), properties] + const style: Style> = [Array.from(strings), properties] const styles = [...stringOrPattern.styles, style] const id = styles.reduce((acc, [strings]) => acc + strings.join(''), '') return { styles: styles, id: 't' + hash(id), tag: stringOrPattern.tag, __pattern__: true } @@ -41,11 +41,18 @@ const construct = (tag: HTMLElements) => { return { styles: styles, id: 't' + hash(id), tag, __pattern__: true } } } + + return create } -const pattern = construct('div') +type HTMLElementsWithoutNull = Exclude +type ConstructorWithTags = Constructor<'div'> & { [K in HTMLElementsWithoutNull]: Constructor } & { global: Constructor } + +const pattern = construct('div') as ConstructorWithTags tags.forEach((tag) => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error pattern[tag] = construct(tag) }) diff --git a/packages/core/src/sheet/index.test.ts b/packages/core/src/sheet/index.test.ts index e6a23a7..36e50bd 100644 --- a/packages/core/src/sheet/index.test.ts +++ b/packages/core/src/sheet/index.test.ts @@ -17,7 +17,7 @@ describe('createStyleSheet CSR', () => { }) describe('createStyleSheet SSR', () => { - beforeAll(() => (global.document = undefined)) + beforeAll(() => (global.document = undefined!)) test('should create a new StyleSheet object', () => { const sheet = createStyleSheet({}) diff --git a/packages/core/src/sheet/tag.ts b/packages/core/src/sheet/tag.ts index 4a72fcd..6e778d0 100644 --- a/packages/core/src/sheet/tag.ts +++ b/packages/core/src/sheet/tag.ts @@ -1,7 +1,7 @@ type Tag = { insertRule(key: string, rule: string): number deleteRule(key: string): void - getRule(key: string): string + getRule(key: string): string | null getAllRules(): string getAllKeys(): string[] [k: string]: unknown @@ -55,7 +55,7 @@ export function createBrowserTag(container?: HTMLElement): Tag { return inserted.size }, deleteRule: function (key: string): void { - rules.removeChild(inserted.get(key)) + rules.removeChild(inserted.get(key)!) inserted.delete(key) }, getRule: function (key: string): string | null { diff --git a/packages/core/src/tags.ts b/packages/core/src/tags.ts index 32ad1e4..1db360b 100644 --- a/packages/core/src/tags.ts +++ b/packages/core/src/tags.ts @@ -131,5 +131,5 @@ const elements = [ 'text', ] as const -export default new Set(elements) -export type HTMLElements = (typeof elements)[number] +export default elements +export type HTMLElements = (typeof elements)[number] | null diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index c3c8fa0..1776826 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -8,7 +8,7 @@ "importHelpers": true, "moduleResolution": "node", "experimentalDecorators": true, - "strict": false, + "strict": true, "strictPropertyInitialization": false, "sourceMap": true, "allowSyntheticDefaultImports": true,