From 9cddf81a09bddc015e9511dfeaa946b9568f981a Mon Sep 17 00:00:00 2001 From: Lloyd Kupchanko Date: Fri, 24 May 2024 23:33:56 -0600 Subject: [PATCH] Fix types for setAll() (#520) Adds overloaded functions for setAll() to allow for optional space and alpha parameters. --- types/src/color.d.ts | 7 ++++--- types/src/setAll.d.ts | 9 ++++++++- types/test/setAll.ts | 8 ++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/types/src/color.d.ts b/types/src/color.d.ts index a1c364a04..7b6d37728 100644 --- a/types/src/color.d.ts +++ b/types/src/color.d.ts @@ -19,7 +19,6 @@ import { equals as equalsFn, get, getAll as getAllFn, - setAll as setAllFn, display, } from "./index-fn.js"; @@ -84,7 +83,6 @@ export type ToColorNamespace any> = T extends ( declare namespace Color { // Functions defined using Color.defineFunctions export const getAll: ToColorNamespace; - export const setAll: ToColorNamespace; export const to: ToColorNamespace; export const equals: ToColorNamespace; export const inGamut: ToColorNamespace; @@ -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 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; } /** @@ -152,7 +152,6 @@ declare class Color extends SpaceAccessors implements PlainColorObject { // Functions defined using Color.defineFunctions get: ToColorPrototype; getAll: ToColorPrototype; - setAll: ToColorPrototype; to: ToColorPrototype; equals: ToColorPrototype; inGamut: ToColorPrototype; @@ -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 number)>): Color; + setAll (coords: Coords, alpha?: number): Color; + setAll (space: string | ColorSpace, coords: Coords, alpha?: number): Color; } export default Color; diff --git a/types/src/setAll.d.ts b/types/src/setAll.d.ts index 9959fa55a..7578e7da4 100644 --- a/types/src/setAll.d.ts +++ b/types/src/setAll.d.ts @@ -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; diff --git a/types/test/setAll.ts b/types/test/setAll.ts index 040e667da..e168aed7b 100644 --- a/types/test/setAll.ts +++ b/types/test/setAll.ts @@ -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( { @@ -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