Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDoc unexpected type error when spreading array containing different shapes of same type #58437

Open
Arkellys opened this issue May 5, 2024 · 3 comments

Comments

@Arkellys
Copy link

Arkellys commented May 5, 2024

πŸ”Ž Search Terms

"typescript jsdoc error spread union", "jsdoc error spread type different shape"

πŸ•— Version & Regression Information

I was unable to test this on prior versions because the TS playground doesn't handle JSDoc

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/FASwdgLgpgTgZgQwMZQAQGED2YAmIIjbqoDewqFqSAXKgM4QzgDmA3OZQEYD8tYUAN1jsAvsFCRYiFBmx4C2AEKkOFJL1T8hMdpVSdaWMHBBtgY4BACeABzRGTzVAF5ZufITDKAPm-mf0cSRsBlRoUNcACmDjU0NsRwBtAF0AShcAPlREABs6KHE4AFcwJAUwVBgoCCKYMABBOgBJCAAeABVbKAzIhBhmWk67VMGulQoqmrrUPrMLAHp51ABRAA87MqgceiKkFDo6SygGSMSSfVpzmlQAIiQb1BFHtPZwiFPzg1IL7+u7h6egJe4jep2AAEhPpcqLR-o9HgAaCFQ75fK6w+7wwHAYHARYrdZQTbbRAgHJHE5nZ6pViofEABRgmDsMGsqAA5Jx2agQHRUABbXl0FgU95UtFYl50paM5mwNmc7m8gVCkV4pYAVX4hOJ2QQZNQAHd8AALVAAKQAygARTBIBE7PbHPnGiBm9qW0VgyE-dG3TGApHggB0ocmtQazTFEJ9Er+AcRMZRyZh-oBWIhaRxNKAA

πŸ’» Code

The playground link contains the TS version, which work as expected, the below is the JSDoc equivalent throwing the unexpected type error:

/**
 * @typedef {object} ConditionC
 * @property {string} c
 * @property {never} [b]
 */

/**
 * @typedef {object} ConditionB
 * @property {never} [c]
 * @property {Config} b
 */

/**
 * @typedef {ConditionB | ConditionC} Config
 */

/**
 * @param {Config[]} config
 * @returns {boolean}
 */
const test = (config) => false;

/**
 * @template Type
 * @param {Type} arg
 * @returns {Type}
 */

function returnAsIt(arg) {
  return arg;
}

// Expected success
test([{ b: { c: "c" } }]);
test([{ b: { b: { c: "c" } } }]);

test([
  { b: { c: "c" } },
  { b: { b: { c: "c" } } }
]);

// Expected fail
test([{ }]); // Property 'b' is missing
test([{ b: {} }]); // Property 'b' is missing

// Unexpected fail
test([
  { b: { c: "c" } },
  ...returnAsIt([
    { b: { c: "c" } },
    { b: { b: { c: "c" } } }
  ])
]);

πŸ™ Actual behavior

With union type and recursion, when using a function that returns the same type as its input, an unexpected type error appear in some cases.

These pass:

test([
  { b: { c: "c" } },
  ...returnAsIt([
    { b: { c: "c" } }
    { b: { c: "c" } }
  ])
]);
test([
  { b: { c: "c" } },
  ...returnAsIt([
     { b: { b: { c: "c" } } }
     { b: { b: { c: "c" } } }
  ])
]);
test([
  { b: { c: "c" } },
  { b: { b: { c: "c" } } },
  ...returnAsIt([
    { b: { c: "c" } },
    { b: { b: { c: "c" } } }
  ])
]);
test([
  { b: { c: "c" } },
  ...([
    { b: { c: "c" } },
    { b: { b: { c: "c" } } }
  ])
]);

But this fail:

test([
  { b: { c: "c" } },
  ...returnAsIt([
    { b: { c: "c" } },
    { b: { b: { c: "c" } } }
  ])
]);
Type '{ b: { c: string; b?: undefined; }; } | { b: { b: { c: string; }; c?: undefined; }; }' is not assignable to type 'Config'.
  Type '{ b: { c: string; b?: undefined; }; }' is not assignable to type 'Config'.
    Property 'c' is missing in type '{ b: { c: string; b?: undefined; }; }' but required in type 'ConditionC'
      'c' is declared here.

πŸ™‚ Expected behavior

I expect that all of the cases above pass as they do in TS, since the types are correct.

Additional information about the issue

VSCode version 1.89.0
TS version 5.4.5

This is tested with checkJs set as true.

@Andarist
Copy link
Contributor

Andarist commented May 6, 2024

It behaves the same way but you need to make sure to enable strictNullChecks for your JS file: TS Playground

I find it confusing that the TS playground for JS files uses strict: true, strictNullChecks: false. When I see strict: true set as the default I certainly don't go check all the other options to see if they are not opting out of strictness. This is done here: https://github.com/microsoft/TypeScript-Website/blob/eaa8205658445d4df6c0ca04e42fab1019f71df0/packages/sandbox/src/compilerOptions.ts#L21

@Arkellys
Copy link
Author

Arkellys commented May 6, 2024

Ah yes you're right, the issue only appears when strictNullCheck is false. Here is the updated playground. I wish I could use this as a workaround for this bug, but turning on strictNullCheck gives me even more errors for things I don't want. 🫀

@Andarist
Copy link
Contributor

Andarist commented May 6, 2024

You can replace your nevers here with undefined and that should fix the problem without you having to turn on strictNullChecks: TS playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants