diff --git a/packages/merge-styles/etc/merge-styles.api.md b/packages/merge-styles/etc/merge-styles.api.md index ae90c9064ab7b..d9f2a075f155f 100644 --- a/packages/merge-styles/etc/merge-styles.api.md +++ b/packages/merge-styles/etc/merge-styles.api.md @@ -65,7 +65,7 @@ export const GLOBAL_STYLESHEET_KEY = "__global__"; // // @public export type IConcatenatedStyleSet = { - [P in keyof Omit_2]: IStyle; + [P in keyof Omit]: IStyle; } & { subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunction; @@ -114,7 +114,7 @@ export type InsertRuleCallback = ({ key, sheet, rule }: InsertRuleArgs) => void; // @public export type IProcessedStyleSet = { - [P in keyof Omit_2]: string; + [P in keyof Omit]: string; } & { subComponentStyles: { [P in keyof TStyleSet['subComponentStyles']]: __MapToFunctionType; @@ -466,7 +466,7 @@ export type IStyleFunctionOrObject = { - [P in keyof Omit_2]: IStyle; + [P in keyof Omit]: IStyle; } & { subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunctionOrObject; @@ -569,12 +569,16 @@ export { Omit_2 as Omit } export function setRTL(isRTL: boolean): void; // @public (undocumented) -export type ShadowConfig = { - stylesheetKey: string; +export interface ShadowConfig { + // (undocumented) + __isShadowConfig__: true; + // (undocumented) inShadow: boolean; + // (undocumented) + stylesheetKey: string; + // (undocumented) window?: Window; - __isShadowConfig__: true; -}; +} // @public (undocumented) export class ShadowDomStylesheet extends Stylesheet { @@ -635,7 +639,7 @@ export const SUPPORTS_MODIFYING_ADOPTED_STYLESHEETS: boolean; // Warnings were encountered during analysis: // // lib/IRawStyle.d.ts:24:9 - (ae-forgotten-export) The symbol "IStyle_2" needs to be exported by the entry point index.d.ts -// lib/IStyleSet.d.ts:61:5 - (ae-forgotten-export) The symbol "__MapToFunctionType" needs to be exported by the entry point index.d.ts +// lib/IStyleSet.d.ts:62:5 - (ae-forgotten-export) The symbol "__MapToFunctionType" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/packages/merge-styles/src/IStyleOptions.ts b/packages/merge-styles/src/IStyleOptions.ts index 75ba01e0115a7..33f0d088153b1 100644 --- a/packages/merge-styles/src/IStyleOptions.ts +++ b/packages/merge-styles/src/IStyleOptions.ts @@ -1,4 +1,4 @@ -import { ShadowConfig } from './shadowConfig'; +import type { ShadowConfig } from './shadowConfig'; import type { Stylesheet } from './Stylesheet'; export interface IStyleOptions { diff --git a/packages/merge-styles/src/IStyleSet.ts b/packages/merge-styles/src/IStyleSet.ts index 7122b5b58d901..857e99c9d04eb 100644 --- a/packages/merge-styles/src/IStyleSet.ts +++ b/packages/merge-styles/src/IStyleSet.ts @@ -12,8 +12,10 @@ export type Diff = ({ [P in T]: P } & /** * @deprecated Use the version provided by TypeScript instead. */ +// eslint-disable-next-line deprecation/deprecation, @typescript-eslint/naming-convention +type _Omit = Pick>; // eslint-disable-next-line deprecation/deprecation -export type Omit = Pick>; +export type { _Omit as Omit }; /** * Helper function whose role is supposed to express that regardless if T is a style object or style function, @@ -38,7 +40,6 @@ export interface IStyleSetBase { * property. */ export type IStyleSet = { - // eslint-disable-next-line deprecation/deprecation [P in keyof Omit]: IStyle; } & { subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunctionOrObject }; @@ -48,7 +49,6 @@ export type IStyleSet * A concatenated style set differs from `IStyleSet` in that subComponentStyles will always be a style function. */ export type IConcatenatedStyleSet = { - // eslint-disable-next-line deprecation/deprecation [P in keyof Omit]: IStyle; } & { subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunction }; @@ -59,7 +59,6 @@ export type IConcatenatedStyleSet = { * into a class name. Additionally, all subComponentStyles are style functions. */ export type IProcessedStyleSet = { - // eslint-disable-next-line deprecation/deprecation [P in keyof Omit]: string; } & { subComponentStyles: { diff --git a/packages/merge-styles/src/concatStyleSetsWithProps.ts b/packages/merge-styles/src/concatStyleSetsWithProps.ts index 2834b38a90988..446189f3c21af 100644 --- a/packages/merge-styles/src/concatStyleSetsWithProps.ts +++ b/packages/merge-styles/src/concatStyleSetsWithProps.ts @@ -12,21 +12,21 @@ export function concatStyleSetsWithProps | undefined)[] ): DeepPartialV2 { - const result: DeepPartialV2[] = []; + const result: Array> = []; for (const styles of allStyles) { if (styles) { result.push(typeof styles === 'function' ? styles(styleProps) : styles); } } if (result.length === 1) { - return result[0] as DeepPartialV2; + return result[0]; } else if (result.length) { // cliffkoh: I cannot figure out how to avoid the cast to any here. // It is something to do with the use of Omit in IStyleSet. // It might not be necessary once Omit becomes part of lib.d.ts (when we remove our own Omit and rely on // the official version). - return concatStyleSets(...result) as any; + return concatStyleSets(...result) as DeepPartialV2; } - return {} as any; + return {} as DeepPartialV2; } diff --git a/packages/merge-styles/src/mergeStyleSets.ts b/packages/merge-styles/src/mergeStyleSets.ts index 485a49f636537..f2fefbafb5926 100644 --- a/packages/merge-styles/src/mergeStyleSets.ts +++ b/packages/merge-styles/src/mergeStyleSets.ts @@ -179,12 +179,12 @@ export function mergeCssSets( * @param options - (optional) Options to use when creating rules. */ export function mergeCssSets(styleSets: any[], options?: IStyleOptions): IProcessedStyleSet { - const classNameSet: IProcessedStyleSet = { subComponentStyles: {} }; + const classNameSet = { subComponentStyles: {} } as IProcessedStyleSet; let shadowConfig: ShadowConfig | undefined = undefined; let styleSet; if (isShadowConfig(styleSets[0])) { - shadowConfig = styleSets[0] as ShadowConfig; + shadowConfig = styleSets[0]; styleSet = styleSets[1]; } else { styleSet = styleSets[0]; @@ -207,7 +207,7 @@ export function mergeCssSets(styleSets: any[], options?: IStyleOptions): IProces for (const styleSetArea in concatenatedStyleSet) { if (concatenatedStyleSet.hasOwnProperty(styleSetArea)) { if (styleSetArea === 'subComponentStyles') { - classNameSet.subComponentStyles = (concatenatedStyleSet as IConcatenatedStyleSet).subComponentStyles || {}; + classNameSet.subComponentStyles = concatenatedStyleSet.subComponentStyles || {}; continue; } else if (styleSetArea === '__shadowConfig__') { continue; @@ -222,12 +222,10 @@ export function mergeCssSets(styleSets: any[], options?: IStyleOptions): IProces if (registration) { registrations.push(registration); - // FIXME: classNameSet invalid types - exposed in TS 4.5 - cast needed - (classNameSet as Record)[styleSetArea] = classes.concat([registration.className]).join(' '); + classNameSet[styleSetArea] = classes.concat([registration.className]).join(' '); } } else { - // FIXME: classNameSet invalid types - exposed in TS 4.5 - cast needed - (classNameSet as Record)[styleSetArea] = classes.join(' '); + classNameSet[styleSetArea] = classes.join(' '); } } } diff --git a/packages/merge-styles/src/shadowConfig.ts b/packages/merge-styles/src/shadowConfig.ts index 0cb6d1f8d647d..4a30066a471da 100644 --- a/packages/merge-styles/src/shadowConfig.ts +++ b/packages/merge-styles/src/shadowConfig.ts @@ -1,9 +1,10 @@ -export type ShadowConfig = { +// eslint-disable-next-line @typescript-eslint/naming-convention +export interface ShadowConfig { stylesheetKey: string; inShadow: boolean; window?: Window; __isShadowConfig__: true; -}; +} export const GLOBAL_STYLESHEET_KEY = '__global__'; export const SHADOW_DOM_STYLESHEET_SETTING = '__shadow_dom_stylesheet__'; @@ -13,7 +14,7 @@ export const DEFAULT_SHADOW_CONFIG: ShadowConfig = { inShadow: false, window: undefined, __isShadowConfig__: true, -} as const; +}; export const makeShadowConfig = (stylesheetKey: string, inShadow: boolean, window?: Window): ShadowConfig => { return { @@ -24,10 +25,14 @@ export const makeShadowConfig = (stylesheetKey: string, inShadow: boolean, windo }; }; -export const isShadowConfig = (obj: unknown): obj is ShadowConfig => { - if (!obj) { +export const isShadowConfig = (value: unknown): value is ShadowConfig => { + if (!(value && isRecord(value))) { return false; } - return (obj as ShadowConfig).__isShadowConfig__ === true; + return value.__isShadowConfig__ === true; }; + +function isRecord(value: unknown): value is Record { + return value !== null && typeof value === 'object' && !Array.isArray(value); +}