Skip to content

Commit

Permalink
Make sure FacetReader instances preserve their output type
Browse files Browse the repository at this point in the history
FIX: Give the tag property on `FacetReader` the type of the output type parameter
to force TypeScript to infer the proper type when converting from `Facet` to
`FacetReader`.

See https://discuss.codemirror.net/t/read-only-facets/7175/7
  • Loading branch information
marijnh committed Oct 18, 2023
1 parent 93b5d08 commit a51def3
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/facet.ts
Expand Up @@ -110,11 +110,9 @@ export class Facet<Input, Output = readonly Input[]> implements FacetReader<Outp
return this.compute([field], state => get!(state.field(field)))
}

tag!: typeof FacetTag
tag!: Output
}

declare const FacetTag: unique symbol

/// A facet reader can be used to fetch the value of a facet, though
/// [`EditorState.facet`](#state.EditorState.facet) or as a dependency
/// in [`Facet.compute`](#state.Facet.compute), but not to define new
Expand All @@ -125,8 +123,9 @@ export type FacetReader<Output> = {
/// @internal
default: Output
/// Dummy tag that makes sure TypeScript doesn't consider all object
/// types as conforming to this type.
tag: typeof FacetTag
/// types as conforming to this type. Not actually present on the
/// object.
tag: Output
}

function sameArray<T>(a: readonly T[], b: readonly T[]) {
Expand Down

0 comments on commit a51def3

Please sign in to comment.