mirror-cockpit-project-cockpit/tsconfig.json
Allison Karlitskaya 75b4087cbf typescript: enable --exactOptionalPropertyTypes
In Typescript when you say

  interface iface {
    prop?: string;
  }

it means that the `iface` type can have an optional property `prop`
present, and if it is present it must be a string *or* `undefined`.  The
reason for that is that given an instance `x` of type `iface`, if you
say `x.prop` then the result of that will be `undefined` if the property
is missing, so in some sense the ideas of "missing" and "`undefined`"
are semi-fungible.

This idea that a property can be present but defined to `undefined` is
incompatible with our definition of JsonObject, which doesn't permit
`undefined` to appear as a value in JSON documents, and has gotten us
into a lot of trouble in the past, trying to create types to describe
things that the bridge sends to us, in cases where a field may or may
not be present.

It seems like the Typescript people changed their mind about this.  In
4.4 they introduced the "recommended" `--exactOptionalPropertyTypes`
flag which doesn't implicitly add `undefined` as a permitted value as a
side-effect of using `?`.

  https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes
  https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#exact-optional-property-types---exactoptionalpropertytypes

This hasn't been made part of `--strict` yet because of how disruptive
it would be.

Let's switch this option on for our code and fix a couple of places
where it causes problems.  One example is the `id` field of
`HelperTextItem`: it's optional, but if present, it must be a string.

Fix a bit of whitespace in tsconfig.json while we're at it.
2024-06-11 17:02:25 +02:00

29 lines
756 B
JSON

/*
* Config for `tsc` and `tsserver`, currently only used for typechecking.
* Our main build is via `esbuild` which can handle `.ts`, but doesn't do any
* checking on its own. To typecheck, run `tsc`.
*
* Extended JSON — comments and trailing commas are good here.
*/
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"baseUrl": "./pkg/lib",
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"jsx": "react",
"lib": [
"dom",
"es2020"
],
"noEmit": true, // we only use `tsc` for type checking
"skipLibCheck": true, // don't check node_modules
"strict": true,
},
"include": [
"pkg/**/*"
],
}