Skip to content

Commit

Permalink
fix(merge-styles): additional typings tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Jun 14, 2024
1 parent 9a73119 commit a30d326
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
20 changes: 12 additions & 8 deletions packages/merge-styles/etc/merge-styles.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const GLOBAL_STYLESHEET_KEY = "__global__";
//
// @public
export type IConcatenatedStyleSet<TStyleSet extends IStyleSetBase> = {
[P in keyof Omit_2<TStyleSet, 'subComponentStyles'>]: IStyle;
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: IStyle;
} & {
subComponentStyles?: {
[P in keyof TStyleSet['subComponentStyles']]: IStyleFunction<any, any>;
Expand Down Expand Up @@ -114,7 +114,7 @@ export type InsertRuleCallback = ({ key, sheet, rule }: InsertRuleArgs) => void;

// @public
export type IProcessedStyleSet<TStyleSet extends IStyleSetBase> = {
[P in keyof Omit_2<TStyleSet, 'subComponentStyles'>]: string;
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: string;
} & {
subComponentStyles: {
[P in keyof TStyleSet['subComponentStyles']]: __MapToFunctionType<TStyleSet['subComponentStyles'] extends infer J ? (P extends keyof J ? J[P] : never) : never>;
Expand Down Expand Up @@ -466,7 +466,7 @@ export type IStyleFunctionOrObject<TStylesProps, TStyleSet extends IStyleSetBase
export type IStyleSet<TStyleSet extends IStyleSetBase = {
[key: string]: any;
}> = {
[P in keyof Omit_2<TStyleSet, 'subComponentStyles'>]: IStyle;
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: IStyle;
} & {
subComponentStyles?: {
[P in keyof TStyleSet['subComponentStyles']]: IStyleFunctionOrObject<any, any>;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion packages/merge-styles/src/IStyleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShadowConfig } from './shadowConfig';
import type { ShadowConfig } from './shadowConfig';
import type { Stylesheet } from './Stylesheet';

export interface IStyleOptions {
Expand Down
7 changes: 3 additions & 4 deletions packages/merge-styles/src/IStyleSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export type Diff<T extends keyof any, U extends keyof any> = ({ [P in T]: P } &
/**
* @deprecated Use the version provided by TypeScript instead.
*/
// eslint-disable-next-line deprecation/deprecation, @typescript-eslint/naming-convention
type _Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>;
// eslint-disable-next-line deprecation/deprecation
export type Omit<U, K extends keyof U> = Pick<U, Diff<keyof U, K>>;
export type { _Omit as Omit };

/**
* Helper function whose role is supposed to express that regardless if T is a style object or style function,
Expand All @@ -38,7 +40,6 @@ export interface IStyleSetBase {
* property.
*/
export type IStyleSet<TStyleSet extends IStyleSetBase = { [key: string]: any }> = {
// eslint-disable-next-line deprecation/deprecation
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: IStyle;
} & {
subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunctionOrObject<any, any> };
Expand All @@ -48,7 +49,6 @@ export type IStyleSet<TStyleSet extends IStyleSetBase = { [key: string]: any }>
* A concatenated style set differs from `IStyleSet` in that subComponentStyles will always be a style function.
*/
export type IConcatenatedStyleSet<TStyleSet extends IStyleSetBase> = {
// eslint-disable-next-line deprecation/deprecation
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: IStyle;
} & {
subComponentStyles?: { [P in keyof TStyleSet['subComponentStyles']]: IStyleFunction<any, any> };
Expand All @@ -59,7 +59,6 @@ export type IConcatenatedStyleSet<TStyleSet extends IStyleSetBase> = {
* into a class name. Additionally, all subComponentStyles are style functions.
*/
export type IProcessedStyleSet<TStyleSet extends IStyleSetBase> = {
// eslint-disable-next-line deprecation/deprecation
[P in keyof Omit<TStyleSet, 'subComponentStyles'>]: string;
} & {
subComponentStyles: {
Expand Down
8 changes: 4 additions & 4 deletions packages/merge-styles/src/concatStyleSetsWithProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ export function concatStyleSetsWithProps<TStyleProps, TStyleSet extends IStyleSe
styleProps: TStyleProps,
...allStyles: (IStyleFunctionOrObject<TStyleProps, TStyleSet> | undefined)[]
): DeepPartialV2<TStyleSet> {
const result: DeepPartialV2<TStyleSet>[] = [];
const result: Array<DeepPartialV2<TStyleSet>> = [];
for (const styles of allStyles) {
if (styles) {
result.push(typeof styles === 'function' ? styles(styleProps) : styles);
}
}
if (result.length === 1) {
return result[0] as DeepPartialV2<TStyleSet>;
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<TStyleSet>;
}

return {} as any;
return {} as DeepPartialV2<TStyleSet>;
}
12 changes: 5 additions & 7 deletions packages/merge-styles/src/mergeStyleSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ export function mergeCssSets<TStyleSet>(
* @param options - (optional) Options to use when creating rules.
*/
export function mergeCssSets(styleSets: any[], options?: IStyleOptions): IProcessedStyleSet<any> {
const classNameSet: IProcessedStyleSet<any> = { subComponentStyles: {} };
const classNameSet = { subComponentStyles: {} } as IProcessedStyleSet<any>;

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];
Expand All @@ -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<any>).subComponentStyles || {};
classNameSet.subComponentStyles = concatenatedStyleSet.subComponentStyles || {};
continue;
} else if (styleSetArea === '__shadowConfig__') {
continue;
Expand All @@ -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<string, any>)[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<string, any>)[styleSetArea] = classes.join(' ');
classNameSet[styleSetArea] = classes.join(' ');
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions packages/merge-styles/src/shadowConfig.ts
Original file line number Diff line number Diff line change
@@ -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__';
Expand All @@ -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 {
Expand All @@ -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<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}

0 comments on commit a30d326

Please sign in to comment.