cloudinary / cloudinary/cloudinary_npm

Typescript: Incorrect usage of Union Typing (mostly options)

Open
#539 7 comments 1 reaction 1 assignee Claimed by @aleksandar-cloudinary View on GitHub
bug
Dominant language
JavaScript
Stars
666
Forks
319
Avg merge
17h 18m
Merged PRs (30d)
3

Description

## Describe the bug in a sentence or two.

Many options type arguments are using [union](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) when the intention is an [intersection](https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types).

## Issue Type (Can be multiple)
* [ ] Build - Can’t install or import the SDK
* [ ] Babel - Babel errors or cross browser issues
* [ ] Performance - Performance issues
* [ ] Behaviour - Functions aren’t working as expected (Such as generate URL)
* [ ] Documentation - Inconsistency between the docs and behaviour
* [x] Incorrect Types - For typescript users who are having problems with our d.ts files
* [ ] Other (Specify)

## Steps to reproduce

An example using the [`ConfigAndUrlOptions`](https://github.com/cloudinary/cloudinary_npm/blob/3e69f9de1be3bb4d4d4ba2fdcb4c8f3e6dd6c6e3/types/index.d.ts#L553) type.

```typescript
import { v2 as cloudinary, ConfigAndUrlOptions } from 'cloudinary';

const options: ConfigAndUrlOptions = {
version: "1",
cloud_name: "something",
// ConfigOptions type is `boolean`, here it is a string
force_version: "true",
};

// look at typescript tooltip for the options object
cloudinary.url('puclic_id', options);
```

This example *will not* fail to compile. However, `force_version` is provided as a string.

## Correct Usage

Instead of a union, the `ConfigAndUrlOptions` object should be an intersection.

```typescript
import { v2 as cloudinary, ConfigOptions, UrlOptions } from 'cloudinary';

type ConfigAndUrlOptions = ConfigOptions & UrlOptions;

const options: ConfigAndUrlOptions = {
version: "1",
cloud_name: "something",
// typescript compile error, wrong type provided
force_version: "true",
};

cloudinary.url('puclic_id', options);
```

## Wrap Up

There are a *lot* of unions being mis-used this way. I'm happy to help create a PR. However, doing so would take significant time for me.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.