Type 'string | number | symbol' is not assignable to type 'string'

Issue

I have a error with following type definition, at L102.

Too long url link to read

TypeScript 4.6.2 sais…

(property) Schema.items?: Schema | undefined
Type 'Schema & p["items"]' does not satisfy the constraint 'Schema'.
  Types of property 'required' are incompatible.
    Type '(readonly (keyof NonNullable<(Schema & p["items"])["properties"]>)[] & readonly (keyof NonNullable<p["items"]["properties"]>)[]) | undefined' is not assignable to type 'readonly string[] | undefined'.
      Type 'readonly (keyof NonNullable<(Schema & p["items"])["properties"]>)[] & readonly (keyof NonNullable<p["items"]["properties"]>)[]' is not assignable to type 'readonly string[] | undefined'.
        Type 'readonly (keyof NonNullable<(Schema & p["items"])["properties"]>)[] & readonly (keyof NonNullable<p["items"]["properties"]>)[]' is not assignable to type 'readonly string[]'.
          The types returned by 'concat(...)' are incompatible between these types.
            Type '(keyof NonNullable<(Schema & p["items"])["properties"]>)[]' is not assignable to type 'string[]'.
              Type 'keyof NonNullable<(Schema & p["items"])["properties"]>' is not assignable to type 'string'.
                Type 'string | number | symbol' is not assignable to type 'string'.
                  Type 'number' is not assignable to type 'string'.(2344)

If I change L34, Schema interface definition, from

    readonly required?: ReadonlyArray<keyof NonNullable<this['properties']>>;

to

    readonly required?: ReadonlyArray<keyof NonNullable<Schema['properties']>>;

that removes the error, but I want to use this.

Is there any way to clear errors while using this?

Solution

Use Extract.

    readonly required?: ReadonlyArray<Extract<keyof NonNullable<this['properties']>, string>>;

https://zenn.dev/jiftechnify/scraps/6f0bdab4be0a8f

Answered By – tamaina

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.