Typescript: Incorrect usage of Union Typing (mostly options)

未关闭
#539 7 条评论 1 个 reaction 已指派 1 人 在 GitHub 查看

@aleksandar-cloudinary 已经在做这个了。

开始于 2023年5月28日。

评估

这个 Issue 还没有评估数据。

描述

bug

Describe the bug in a sentence or two.

Many options type arguments are using union when the intention is an intersection.

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
  • Incorrect Types - For typescript users who are having problems with our d.ts files
  • Other (Specify)

Steps to reproduce

An example using the ConfigAndUrlOptions type.

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.

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.

主要语言
JavaScript
星标
672
派生
318
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudinary/cloudinary_npm 的其他 Issue

查看 cloudinary/cloudinary_npm 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。