mapbox / mapbox/mapbox-gl-js

generated types incompatible with exactOptionalPropertyTypes

Open
#13,389 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug :lady_beetle:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

the generated types contain some contradictions which typescript does not detect due to the `strict` flag in the tsconfig.json being set to `false`.

this is a problem when a consuming project tries to enable `exactOptionalPropertyTypes`: now typescript considers these contradictions to be hard errors and refuses to compile.

one example are VectorTileSource and the interface ISource it implements:

ISource says "the prepare property does not have to exist, but if it exists, it has to be a function."
```
interface ISource extends Evented {
...
readonly prepare?: () => void;
...
}
```

On the other hand VectorTileSource says "the property prepage must exists, and it has to be a value of type undefined.
```
export declare class VectorTileSource extends Evented implements ISource {
...
prepare: undefined;
....
}
```

i'm not sure why VectorTileSource requires a prepare property to exist - if this is not strictly necessary, just removing the prepare property would solve the issue. May be confusing for a user who sets prepare and it unexpectedly does not do anything, though.

another way to solve this would be to change ISource do use `readonly prepare?: () => void|undefined;` which is a bit unsatisfying because now there are two different ways to express that prepare is not a function which should be called.

in general i think a typescript library should strive for the most strict typescript variant as not to limit how strict a consuming project wants to be configured.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tsconfig.json and the generated declarations for ISource and VectorTileSource, focusing on the prepare property and other optional-property contradictions. Check the declarations with exactOptionalPropertyTypes enabled in a TypeScript consumer configuration; done means the generated types compile without these incompatibilities while preserving the intended API behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.