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

[@smui/common] TypeError for projects with exactOptionalPropertyTypes: true #664

Open
Tracked by #1741 ...
marekdedic opened this issue Sep 12, 2024 · 1 comment
Open
Tracked by #1741 ...
Labels
bug Something isn't working

Comments

@marekdedic
Copy link

Describe the bug
Hello, I have a TS project using SMUI and I want to turn on the exactOptionalPropertyTypes tsconfig option. The trouble is, since SMUI distributes TS source files, those get checked by the TS compiler as well, so the extra check gets applied to them as well... and it fails.

I would like to stress that this is not a false positive - the extra check uncovers what is really a potential issue in @smui/common. See the following line:

const event: CustomEvent<T> = new CustomEvent(eventType, {
...eventInit,
detail,
});

Here the new CustomEvent infers its T type from the type of the detail property - which with the extra check is more strictly inferred as T | undefined - this is IMHO correct, because the property is marked as optional in the function signature...

To Reproduce
Steps to reproduce the behavior:

  1. Use @smui/common in a project (install it from NPM)
  2. Turn on exactOptionalPropertyTypes

Expected behavior
No error

Actual behavior

/home/user/project/node_modules/@smui/common/src/internal/dispatch.ts:15:9
Error: Type 'CustomEvent<T | undefined>' is not assignable to type 'CustomEvent<T>'.
  Type 'T | undefined' is not assignable to type 'T'.
    'T | undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
      Type 'undefined' is not assignable to type 'T'.
        'undefined' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
  }
  const event: CustomEvent<T> = new CustomEvent(eventType, {
    ...eventInit,


/home/user/project/node_modules/@smui/common/src/internal/dispatch.ts:21:11
Error: Type 'CustomEvent<T | undefined>' is not assignable to type 'CustomEvent<T>'.
  if (duplicateEventForMDC && eventType.startsWith('SMUI')) {
    const duplicateEvent: CustomEvent<T> = new CustomEvent(
      eventType.replace(/^SMUI/g, () => 'MDC'),

Desktop (please complete the following information):

  • OS: Ubuntu 22.04
@marekdedic marekdedic added the bug Something isn't working label Sep 12, 2024
@marekdedic
Copy link
Author

I tried fixing it, an easy fix would be to replace T with T | undefined i these two lines:

const event: CustomEvent<T> = new CustomEvent(eventType, {

const duplicateEvent: CustomEvent<T> = new CustomEvent(

  • but I think you would probably want to turn this option on in your tsconfig as well? And I don't really know if this fix captures the semantics of what you want to do...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant