Skip to content

Erroneous type error with create-react-app and noUncheckedIndexedAccess #144

Closed
@atomanyih

Description

@atomanyih

Describe the bug
If you use the noUncheckedIndexedAccess compiler flag, classnames that are defined will still be typed as string | undefined

To Reproduce
Repro repo: https://github.com/atomanyih/ts-css-modules-test

If you npm start or npx tsc you will see this type error:

error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

12   const className = someFunction(styles.test);
                                    ~~~~~~~~~~~

Expected behavior
The generated classname should be defined

Additional context
Use-cases where this is annoying:

  • using classnames
    cx({[styles.someClass]: someCondition}) will error.
    Workaround: cx(someCondition && styles.someClass) will pass
  • external packages that don't accept undefined
type someFunction = (className: string) => void

someFunction(styles.someClass) // will error

Workaround: someFunction(styles.someClass as string) (kind of whack)

I'm migrating a codebase to use the noUncheckedIndexedAccess flag and this issue is widely present in the codebase. I am able to work around it, but it's not super ideal

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions