Skip to content

Commit

Permalink
Fix types for setAll()
Browse files Browse the repository at this point in the history
Adds overloaded functions for setAll() to allow for optional
space and alpha parameters.
  • Loading branch information
lloydk committed May 23, 2024
1 parent 185ff1e commit 42cb581
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 4 additions & 3 deletions types/src/color.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
equals as equalsFn,
get,
getAll as getAllFn,
setAll as setAllFn,
display,
} from "./index-fn.js";

Expand Down Expand Up @@ -84,7 +83,6 @@ export type ToColorNamespace<T extends (...args: any[]) => any> = T extends (
declare namespace Color {
// Functions defined using Color.defineFunctions
export const getAll: ToColorNamespace<typeof getAllFn>;
export const setAll: ToColorNamespace<typeof setAllFn>;
export const to: ToColorNamespace<typeof toFn>;
export const equals: ToColorNamespace<typeof equalsFn>;
export const inGamut: ToColorNamespace<typeof inGamutFn>;
Expand All @@ -100,6 +98,8 @@ declare namespace Color {
// These should always match the signature of the original function
export function set (color: ColorTypes, prop: Ref, value: number | ((coord: number) => number)): Color;
export function set (color: ColorTypes, props: Record<string, number | ((coord: number) => number)>): Color;
export function setAll (color: ColorTypes, coords: Coords, alpha?: number): Color;
export function setAll (color: ColorTypes, space: string | ColorSpace, coords: Coords, alpha?: number): Color;
}

/**
Expand Down Expand Up @@ -152,7 +152,6 @@ declare class Color extends SpaceAccessors implements PlainColorObject {
// Functions defined using Color.defineFunctions
get: ToColorPrototype<typeof get>;
getAll: ToColorPrototype<typeof getAllFn>;
setAll: ToColorPrototype<typeof setAllFn>;
to: ToColorPrototype<typeof toFn>;
equals: ToColorPrototype<typeof equalsFn>;
inGamut: ToColorPrototype<typeof inGamutFn>;
Expand All @@ -164,6 +163,8 @@ declare class Color extends SpaceAccessors implements PlainColorObject {
// These should always match the signature of the original function
set (prop: Ref, value: number | ((coord: number) => number)): Color;
set (props: Record<string, number | ((coord: number) => number)>): Color;
setAll (coords: Coords, alpha?: number): Color;
setAll (space: string | ColorSpace, coords: Coords, alpha?: number): Color;
}

export default Color;
9 changes: 8 additions & 1 deletion types/src/setAll.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ declare namespace setAll {
let returns: "color";
}

declare function setAll (
color: ColorTypes,
coords: [number, number, number],
alpha?: number | undefined
): PlainColorObject;

declare function setAll (
color: ColorTypes,
space: string | ColorSpace,
coords: [number, number, number]
coords: [number, number, number],
alpha?: number | undefined
): PlainColorObject;

export default setAll;
8 changes: 8 additions & 0 deletions types/test/setAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ setAll(new Color("red"), sRGB);

setAll(new Color("red"), "srgb", [1, 2, 3]); // $ExpectType PlainColorObject
setAll(new Color("red"), sRGB, [1, 2, 3]); // $ExpectType PlainColorObject

setAll("red", "srgb", [1, 2, 3], 0.5); // $ExpectType PlainColorObject
setAll("red", [1, 2, 3]); // $ExpectType PlainColorObject
setAll("red", [1, 2, 3], 0.5); // $ExpectType PlainColorObject

// $ExpectType PlainColorObject
setAll(
{
Expand All @@ -26,5 +31,8 @@ setAll(

new Color("red").setAll("srgb", [1, 2, 3]); // $ExpectType Color
new Color("red").setAll(sRGB, [1, 2, 3]); // $ExpectType Color
new Color("red").setAll("srgb", [1, 2, 3], 0.5); // $ExpectType Color
Color.setAll("red", "srgb", [1, 2, 3]); // $ExpectType Color
Color.setAll(new Color("red"), "srgb", [1, 2, 3]); // $ExpectType Color
Color.setAll("red", "srgb", [1, 2, 3], 0.5); // $ExpectType Color
Color.setAll("red", [1, 2, 3], 0.5); // $ExpectType Color

0 comments on commit 42cb581

Please sign in to comment.