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

Using cockpit with strictNullCheck = true results in not assignable errors #21352

Closed
kingsleyzissou opened this issue Nov 28, 2024 · 5 comments
Closed
Labels

Comments

@kingsleyzissou
Copy link

Explain what happens

I am importing the pkg/lib directory into an existing react/ts project (https://github.com/osbuild/image-builder-frontend).

We have strictNullCheck set to true in our tsconfig. This results in a number of errors in the pkg/lib/cockpit/channel.ts file, I'll just copy the first error for the sake of brevity.

pkg/lib/cockpit/channel.ts:31:3 - error TS2411: Property 'command' of type 'undefined' is not assignable to 'string' index type 'JsonValue'.

31   command?: never;
     ~~~~~~~

Found 7 errors in the same file, starting at: pkg/lib/cockpit/channel.ts:31

When I check the type for JSONValue I see:

export type JsonValue = | null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };

Which is an issue when strictNullCheckis set to true since undefined !== null.

Version of Cockpit

main

Where is the problem in Cockpit?

None

Server operating system

Red Hat Enterprise Linux

Server operating system version

No response

What browsers are you using?

Chrome

System log

No response

@kingsleyzissou
Copy link
Author

@allisonkarlitskaya
Copy link
Member

allisonkarlitskaya commented Nov 28, 2024

Please try also enabling

"exactOptionalPropertyTypes": true

in your tsconfig.json and let me know if it helps.

The issue here is that semantically command? should mean "command may or may not exist" but without exactOptionalPropertyTypes that's treated as equivalent to "the value of command may be undefined", which clearly runs headlong into our JSON type not permitting undefined.

See https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes

Note also that this flag is marked as "Recommended". It's frustrating that it's not enabled by default. :(

@kingsleyzissou
Copy link
Author

Thanks yeah it does indeed get rid of those errors. Unfortunately OpenAPI codegen doesn't seem to support the flag yet (OpenAPITools/openapi-generator#14793).

I might be able to workaround it, will have to look.

@allisonkarlitskaya
Copy link
Member

One thing we could address with this issue: documenting what tsconfig options we expect our consumers to use. It might also be worth asking the tsc folks why this option isn't the default by now...

@kingsleyzissou
Copy link
Author

One thing we could address with this issue: documenting what tsconfig options we expect our consumers to use. It might also be worth asking the tsc folks why this option isn't the default by now...

Thanks, in the end it was okay. I just had a lot of errors to work through but I was able to configure our generated code to generate union undefined types

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

No branches or pull requests

2 participants