microsoft / microsoft/TypeScript

Optional type as required if used in one branch of nested promises

Ouverte
#42,293 2 commentaires 0 réactions 1 personne assignée Voir sur GitHub

@rbuckton y travaille déjà.

Depuis le 12/1/2021.

Needs Investigation Rescheduled
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Bug Report

The system falsely reports an optional property as required if one branch returns the one optional property, but a second branch skips it and returns another optional property.

🔎 Search Terms

promise, catch, optional property

🕗 Version & Regression Information
  • 3.1.4
  • nightly (4.2.0-dev.20210112)
⏯ Playground Link

This playground shows 3 examples of the code posted below, where I commented out specific parts of the code. If you remove the comments in a variant, it will show the error:

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcAiBDGG4F44AKUEAtgJYDOwAPAN4BQcTcUwAjgK5msAmREKWIgBccAHYcSAI2BQA3I2YCYZCGIwAbfoKQBGAPyiJ02QuZxlq9VuI7EAJkPjJM+fQC+APjNx6AYzUKeAA3DCgyDCkNYFFMbDxCYnIqADpWCggNYOAACnsASnoUmAALYDEcnLBiACtgPxh8vE84BiYyADM4HIBCaog6hqa25lYYDigxVsVzVk5uYD5bWSRRAAYAGhmlMBU1TW0VxF1Re03t9x93OGANKmnzAHpHsYmp-mTgNOAMrNyAbQAuvlimUKjkmrgWiNZsBxpMHuZRuwuLxDkJ1lskTs9tZ0Uh7KJdOdsZdts93Pkrh5Cik-Fg-CVKpCWjkRpZ9jYBEcTnBiViWCiFktuRi4OdKVT6P5AiEwhEosBCegsDh8B9KF90plsnlaaVypV+oNGs1EZ1un1avVTTDBfCpnamHNUYt8SJxQKno8OXjlkJeWcvXAyUxrrd7nbXgiNaltX8ckCQQbwSzEbCHemkS7he7MdtzL6Dv6CUSSUjQ8xKdTKUV6TBGcyzWzFEWuXZefzFDm0SWPRL8lKZWIgnBQuFItEAMyxVUJWNan463IFIopo3WoZmkYW3rGm3DbbRx0FwXzXui1ae09t92B8thq43O6oKNwt6JUia76-XVJ0GGhC26nseWawuebp9vm2JMLefbKl2pI+GGUphjS9DPHSDJMkBULdAwzywbsVjFpexxllshFnq6Ip2PmFKDgow6-F8GgQAA5jk44KtEGxjvKk5Knx3GCVOhRAA

💻 Code
export type Data = Promise<{
    requiredProperty: number;
    optionalProperty1?: number;
    optionalProperty2?: number;
}>;
  
const variable: Data = Promise.resolve(2)
.then((project) => {
  if (!project) {
    return {
      requiredProperty: 0,
      optionalProperty1: 20,
    };
  } else {
    return Promise.resolve([]).then(() => {
      return {
        requiredProperty: 0,
        optionalProperty2: 10,
      };
    });
  }
})
.catch(() => ({
  optionalProperty1: 10,
  requiredProperty: 0,
}));
🙁 Actual behavior
⠏ (3/3) Building...
test.ts:7:7 - error TS2322: Type 'Promise<number | { optionalProperty1: number; requiredProperty: number; }>' is not assignable to type 'Data'.
  Type 'number | { optionalProperty1: number; requiredProperty: number; }' is not assignable to type '{ requiredProperty: number; optionalProperty1?: number | undefined; optionalProperty2?: number | undefined; }'.
    Type 'number' is not assignable to type '{ requiredProperty: number; optionalProperty1?: number | undefined; optionalProperty2?: number | undefined; }'.

7 const variable: Data = Promise.resolve(2)
        ~~~~~~~~


test.ts:8:7 - error TS2345: Argument of type '(project: number) => Promise<{ requiredProperty: number; optionalProperty2: number; }> | { requiredProperty: number; optionalProperty1: number; }' is not assignable to parameter of type '(value: number) => { requiredProperty: number; optionalProperty2: number; } | PromiseLike<{ requiredProperty: number; optionalProperty2: number; }>'.
  Type 'Promise<{ requiredProperty: number; optionalProperty2: number; }> | { requiredProperty: number; optionalProperty1: number; }' is not assignable to type '{ requiredProperty: number; optionalProperty2: number; } | PromiseLike<{ requiredProperty: number; optionalProperty2: number; }>'.
    Type '{ requiredProperty: number; optionalProperty1: number; }' is not assignable to type '{ requiredProperty: number; optionalProperty2: number; } | PromiseLike<{ requiredProperty: number; optionalProperty2: number; }>'.
      Property 'optionalProperty2' is missing in type '{ requiredProperty: number; optionalProperty1: number; }' but required in type '{ requiredProperty: number; optionalProperty2: number; }'.

8 .then((project) => {
        ~~~~~~~~~~~~~~

  test.ts:18:9
    18         optionalProperty2: 10,
               ~~~~~~~~~~~~~~~~~~~~~
    'optionalProperty2' is declared here.

ℹ (3/3) Building...found 2 errors
🙂 Expected behavior

Should work without reporting any errors.

Please explain if this was intended in any way.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.