Skip to content

Commit 69d8aa7

Browse files
authored
Improve the return type of cloudinary.v2.config() in TypeScript (#494)
* Improve the return type of cloudinary.v2.config() - The original typescript definition was to return void, but the actual code always returns something, either the config or a property. - Calls will generally return the ConfigOptions const config = cloudinary.v2.config() - A string parameter works like a getter, returning the property cloudinary.v2.config("private_cdn") === true - Two parameters acts like a setter, but returns the config object const config = cloudinary.v2.config("private_cdn", true) - If the function gets two parameters, but the 2nd is undefined, it acts like the getter instead of setting the config key to undefined cloudinary.v2.config("private_cdn", undefined) === true
1 parent 66212b0 commit 69d8aa7

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

types/cloudinary_ts_spec.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
import * as cloudinary from 'cloudinary';
22
import * as Http from "http";
33

4-
// $ExpectType void
5-
cloudinary.v2.config({
6-
cloud_name: 'demo',
7-
});
4+
// $ExpectType ConfigOptions
5+
cloudinary.v2.config();
86

9-
// $ExpectType void
7+
// $ExpectType ConfigOptions
108
cloudinary.v2.config(true);
119

12-
// $ExpectType void
13-
cloudinary.v2.config("private_cdn", true);
10+
// $ExpectType ConfigOptions
11+
cloudinary.v2.config({ cloud_name: "demo" });
12+
13+
// $ExpectError
14+
cloudinary.v2.config({ cloud_name: 0 });
15+
16+
// $ExpectType boolean | undefined
17+
cloudinary.v2.config("private_cdn");
18+
19+
// $ExpectType string | undefined
20+
cloudinary.v2.config("cloud_name", undefined);
21+
22+
// $ExpectType any
23+
cloudinary.v2.config("not_a_key");
24+
25+
// $ExpectType boolean | undefined
26+
cloudinary.v2.config(true).private_cdn;
27+
28+
// $ExpectType boolean
29+
cloudinary.v2.config("private_cdn", true).private_cdn;
30+
31+
// $ExpectType string | undefined
32+
cloudinary.v2.config(true).cloud_name;
33+
34+
// $ExpectType string
35+
cloudinary.v2.config("cloud_name", "foo").cloud_name;
36+
37+
// $ExpectError
38+
cloudinary.v2.config("private_cdn", true, "extra_parameter");
1439

1540
// $ExpectType string
1641
const test = cloudinary.v2.image("front_face.png", {

types/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,11 @@ declare module 'cloudinary' {
686686

687687
function cloudinary_js_config(): string;
688688

689-
function config(new_config: ConfigOptions | string, new_value?: string | boolean): void;
689+
function config(new_config?: boolean | ConfigOptions): ConfigOptions;
690690

691-
function config(new_config: boolean | object): void;
691+
function config<K extends keyof ConfigOptions, V extends ConfigOptions[K]>(key: K, value?: undefined): V;
692+
693+
function config<K extends keyof ConfigOptions, V extends ConfigOptions[K]>(key: K, value: V): ConfigOptions & { [Property in K]: V }
692694

693695
function url(public_id: string, options?: TransformationOptions | ConfigAndUrlOptions): string;
694696

0 commit comments

Comments
 (0)