Repository navigation
Commit 1c5aca0
authored
fix: make the Exact helper compile on TypeScript 7.1 (#1293)
### Symptom
Every generated file that uses `useExactTypes` (the default) fails to
compile on TypeScript 7.1 (checked with `7.1.0-beta` and
`7.1.0-dev.20261020`):
```
error TS2344: Type 'I[K]' does not satisfy the constraint 'P[K]'.
Type 'I' is not assignable to type 'P'.
'I' is only assignable to the non-distributed 'P', but 'P' has been distributed here.
error TS2536: Type 'K' cannot be used to index type 'I'.
```
TypeScript 5.9 and 7.0 accept the same code.
### Cause
```ts
type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
```
The conditional distributes over `P`, so in the false branch `P` is a
single union member, while the constraint `I extends P` still refers to
the whole union. Since microsoft/TypeScript#64237 (merged for 7.1) the
compiler distinguishes the distributed `P` from the non-distributed one,
so `I[K]` (with `K extends keyof <distributed P>`) is no longer
considered a valid index into `I`, and `I[K]` no longer satisfies
`P[K]`.
This is intentional rather than a beta bug: the old alias was unsound
(`I extends Child | undefined` does not guarantee that `I` has the keys
of `Child`), and that PR uses the same shape, `type Issue<A, B extends
A> = A extends unknown ? Show<A, B> : never`, as its example of a newly
reported error. microsoft/TypeScript#64673 and its fix #64307 concern a
separate regression from the same change (identical generic signatures
no longer relating) and do not cover this pattern; the latest 7.1
nightly still rejects it.
### Fix
```ts
type Exact<P, I> = P extends Builtin ? P
: P & { [K in keyof P as Extract<K, keyof I>]: Exact<P[K], I[K & keyof I]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
```
* `I` is no longer constrained by the alias. `fromPartial`/`create`
already constrain `I extends Exact<DeepPartial<T>, I>`, so nothing
changes for callers.
* The mapped type keeps only the keys of `P` that `I` actually has,
which lets `I[K & keyof I]` type-check. This produces the same checks as
before: for keys missing from `I` the old `I[K]` resolved to `unknown`,
i.e. no excess-property check for that key, and `P &` still contributes
every property. Union fields (`oneof=unions`), `Builtin`s,
`useUnknownForValue` and `$type` behave as before.
Alternatives that were tried and rejected: `K extends keyof I ? I[K] :
unknown` breaks the nested `Child.fromPartial(object.child)` calls
inside generated code (deferred conditional types no longer relate
across the generic `I`s), and any `(I & X)[K]` form produces "circularly
references itself" errors.
### Testing
* New type tests (`@ts-expect-error` for unknown properties at the top
level, in nested messages, in repeated fields and inside `oneof` cases,
plus compiling valid partials) in `integration/simple`,
`integration/oneof-unions` and `integration/value-unknown`.
* `yarn proto2ts` re-generated all integration output; `yarn tsc:check`
and `yarn test` pass on TypeScript 5.9.3.
* `integration/**/*.ts` and `integration/**/*-test.ts` type-check on
`[email protected]` (with the removed `esModuleInterop` option
dropped from the tsconfig). The only remaining 7.1 errors are the
pre-existing `new DataLoader(...)` calls on a namespace import in
`batching-with-context` and `lower-case-svc-methods`, which are
unrelated to this change.1 parent 1b195c0 commit 1c5aca0
266 files changed
Lines changed: 1377 additions & 526 deletions
File tree
- integration
- affixes
- google/protobuf
- angular
- any-json-without-registry
- google/protobuf
- any-type-json
- google/protobuf
- any-type-url-json
- google/protobuf
- async-iterable-services-abort-signal
- async-iterable-services
- avoid-import-conflicts-folder-name
- ui
- avoid-import-conflicts
- barrel-imports
- batching-with-context-esModuleInterop
- batching-with-context
- batching
- before-after-request-streaming
- google/protobuf
- before-after-request
- bytes-as-base64-browser
- bytes-as-base64-node
- bytes-as-base64
- bytes-node
- google/protobuf
- const-enum
- default-with-custom-metadata
- default-with-metadata
- emit-default-values-json
- google/protobuf
- enums-as-literals-with-string-enums
- enums-as-literals
- extension-import
- extensions
- fieldmask-optional-all
- google/protobuf
- fieldmask
- google/protobuf
- fieldoption-jstype-with-forcelong-bigint
- fieldoption-jstype-with-forcelong-long
- fieldoption-jstype-with-forcelong-number
- fieldoption-jstype-with-forcelong-string
- fieldoption-jstype
- file-suffix
- google/protobuf
- from-partial-no-initialize
- generic-metadata
- generic-service-definitions-and-services
- generic-service-definitions
- global-this
- groups
- grpc-js-outputschema
- grpc-js-use-date-false
- google/protobuf
- grpc-js-use-date-string-nano
- google/protobuf
- grpc-js-use-date-string
- google/protobuf
- grpc-js-use-date-temporal-bigint
- google/protobuf
- grpc-js-use-date-temporal
- google/protobuf
- grpc-js-use-date-true
- google/protobuf
- grpc-js
- google/protobuf
- grpc-web-abort-signal
- grpc-web-go-server
- grpc-web-no-streaming-observable
- grpc-web-no-streaming
- grpc-web
- google/protobuf
- handle-error-in-default-service
- handle-error-with-after-response
- import-mapping
- google/protobuf
- import-suffix
- google/protobuf
- isolated-declarations-grpc-js
- isolated-declarations
- google/protobuf
- lower-case-svc-methods
- map-bigint-optional
- map-long-optional
- map-longstring-optional
- message-enum-affixes
- google/protobuf
- nested-repeated-values-optional-all
- google/protobuf
- no-comments
- no-defaults-for-optionals-with-nulls
- no-defaults-for-optionals
- no-proto-package
- non-finite-floats
- google/protobuf
- omit-optionals
- oneof-properties
- oneof-unions-no-optionals
- oneof-unions-snake
- google/protobuf
- oneof-unions-value
- google/protobuf
- oneof-unions
- google/protobuf
- optional-long
- google/protobuf
- optional-type-definitions
- optional-type-registry
- output-file-name
- sub
- output-index
- outputschema-const-with-file-suffix
- point
- proto-json-format
- proto2-long
- proto2-no-default-vals
- proto2-no-optionals
- proto2
- remove-enum-prefix-string-enums
- remove-enum-prefix-unrecognized-enum-value
- remove-enum-prefix
- reserved-words
- return-observable
- schema-no-file-descriptor
- simple-deprecated-fields
- simple-esmodule-interop
- simple-json-name
- google/protobuf
- simple-long-bigint-noliteral
- google/protobuf
- simple-long-bigint
- google/protobuf
- simple-long-string
- google/protobuf
- simple-long
- google/protobuf
- simple-optionals
- google/protobuf
- import_dir
- simple-proto2
- simple-prototype-defaults
- google
- protobuf
- type
- import_dir
- simple-snake
- google/protobuf
- import_dir
- simple-string-enums
- google/protobuf
- simple-unrecognized-enum
- google/protobuf
- import_dir
- simple-with-context-no-dataloaders
- simple
- google
- protobuf
- type
- import_dir
- static-only-type-registry
- bar
- google/protobuf
- static-only
- bar
- google/protobuf
- struct
- google/protobuf
- suffix-type
- google/protobuf
- suffix-usedate
- google/protobuf
- type-annotations
- bar
- google/protobuf
- type-registry
- bar
- google/protobuf
- types-with-underscores
- use-date-false
- google/protobuf
- use-date-string
- google/protobuf
- use-date-true
- google/protobuf
- use-duration-bigint
- google/protobuf
- use-duration-forcelong-string
- google/protobuf
- use-duration-long
- google/protobuf
- use-duration
- google/protobuf
- use-json-name-oneof-unions
- use-json-name-struct
- google/protobuf
- use-json-name
- google/protobuf
- use-json-timestamp-raw
- google/protobuf
- use-map-type
- google/protobuf
- use-null-as-optional
- use-numeric-enum-json
- google/protobuf
- use-objectid-true-external-import
- objectid
- use-optionals-all
- google/protobuf
- use-optionals-deprecated-only
- use-optionals-no-undefined
- google/protobuf
- use-readonly-types
- google/protobuf
- value-unknown
- google/protobuf
- value
- google/protobuf
- vector-tile
- wrapper-bigint-json
- google/protobuf
- wrapper-long-json
- google/protobuf
- wrappers-regression/google/protobuf
- src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments