Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

wip: strictly types fields #43

Closed
wants to merge 1 commit into from
Closed

wip: strictly types fields #43

wants to merge 1 commit into from

Conversation

jacobsfletch
Copy link
Member

Resolves #34 by strictly typing fields.

HELP NEEDED

I can't seem to get these stricter types to resolve in my intersection. Basically I have an object with known properties of Type A and unknown properties of Type B. Here's a very simple breakdown of my exact issue:

export type BaseFields = {
  text: boolean | Partial<Field>
} & {
  [key: string]:
    Block
    | ((fieldConfig?: FieldConfig) => Block)
}

const baseFields: BaseFields = {
  text: true,
}

// Produces this error
// Property 'text' is incompatible with index signature.
//       Type 'boolean' is not assignable to type 'Block | ((fieldConfig?: FieldConfig) => Block)'.

// Note: Using `Exclude` on the union type also does not work, i.e. [key: Exclude<string, 'text'>]: Block | (fieldConfig?: FieldConfig) => Block

Here's the exact errors I'm running into with this code:

Screenshot 2023-05-15 at 5 05 41 PM
Screenshot 2023-05-15 at 5 05 32 PM

I found a related GitHub discussion here: microsoft/TypeScript#20597

@IAmNatch
Copy link

IAmNatch commented Aug 3, 2023

Seems to be impossible at this time without either:
a) runtime checks
b) adding the text union value to the block union (even though its wrong), thus being a subset of the main type instead of a conflict.

@jacobsfletch jacobsfletch deleted the fix/field-types branch December 12, 2023 22:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require slug in Partial<Block> type
2 participants