question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MantineThemeColors are possibly "undefined" (loses types on TypeScript v4.8.2)

See original GitHub issue

What package has an issue

@mantine/core

Describe the bug

I have a project that uses TypeScript 4.7.4 and compiles just fine. However, I recently noticed VS Code (which is using TypeScript 4.8.2) has started reporting errors for MantineThemeColors. At first, I thought it was a problem with my VS Code, but after upgrading my project’s TS version to 4.8.2, the compilation started failing.

Running tsc --version && tsc --no-emit with TS 4.7.4 (no errors):

Version 4.7.4

Running tsc --version && tsc --no-emit with TS 4.8.2:

Version 4.8.2
theme.colors.dark[9]
                ^
Object is possibly "undefined".

tsconfig.json

{
    "compilerOptions": {
        "incremental": true,
        "target": "ESNext",
        "module": "ESNext",
        "outDir": "./dist",
        "moduleResolution": "node",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "skipLibCheck": true,
        "resolveJsonModule": true,
        "baseUrl": ".",
        "allowJs": true,
        "sourceMap": true,
        "preserveWatchOutput": true,
        "pretty": true,
        "newLine": "LF",
        "types": ["node"],
        "jsx": "react",
        "strict": true,
        "exactOptionalPropertyTypes": false,
        "noFallthroughCasesInSwitch": true,
        "noImplicitAny": true,
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noUncheckedIndexedAccess": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "forceConsistentCasingInFileNames": true
    },
    "include": ["src", "vite.config.ts"]
}

What version of @mantine/hooks page do you have in package.json?

5.2.5

If possible, please include a link to a codesandbox with the reproduced problem

Unable to create a codesandbox, since they don’t seem to allow running NPM scripts and the IDE’s TS version is stuck at 4.5.5 .

Do you know how to fix the issue

No

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

Removing (string & {}) from DefaultMantineColor seems to fix the type (i.e. I don’t get “possibly undefined” errors). However, it causes errors in other places, such as ThemeIcon.color which would no longer accept a string. (I’d sometimes use gray.0 instead of the theme object from useMantineTheme. There could be other errors that I have not encountered.

The alternative right now would be to simply use optional chaining.

theme.colors.dark?.[9]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
maxijonsoncommented, Sep 9, 2022

A PR has been created in TypeScript and merged, this will be fixed in 4.9.0!

1reaction
rtivitalcommented, Sep 8, 2022

I’m closing this issue as it seems not to be related to Mantine, if you think that there is something to be done on our side, then feel free to reopen it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Since 4.8.2, union discrimination fails with `(string ... - GitHub
TypeScript 4.8.2 should only report that an object's property is possibly "undefined" for un-declared fields, as stated in the documentation.
Read more >
"Object is possibly 'undefined'." in TypeScript - Stack Overflow
The easiest way to do that is to use a new variable: // over here the compiler knows obj is defined const constObj...
Read more >
Documentation - TypeScript 4.4
Exact Optional Property Types ( --exactOptionalPropertyTypes ). In JavaScript, reading a missing property on an object produces the value undefined . It's also ......
Read more >
Even after type narrowing I get: Object is possibly 'undefined ...
I've found that TypeScript is most useful for ensuring types are compatible across an application or ecosystem. In the context of a single ......
Read more >
TIL: Destructuring from an Object with a possibly Undefined ...
Destructure from a possibly undefined object property ... the result could be a User object or undefined , so I got the following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found