ts-essentials / ts-essentials

All essential TypeScript types in one place 🤙

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ts-essentials breaks with keyofStringsOnly typescript compiler option

MarcCelani-at opened this issue · comments

This issue was first reported in #46, and the user just removed the compiler option in their project, which does not work for us.

This is the error when typechecking our project with skipLibCheck=false and keyofStringsOnly=true

node_modules/ts-essentials/dist/types.d.ts:5:13 - error TS2322: Type 'K' is not assignable to type 'string'.
  Type 'string | number' is not assignable to type 'string'.
    Type 'number' is not assignable to type 'string'.

      [key in K]: T;

The error appears to go away if I patch Dictionary to be...

export declare type Dictionary<T, K extends keyof any = string> = {
    [key in K]: T;
};

Of course, if you do not want to allow symbols, you may be able to do something like keyof any & (string | number)?

Hey @MarcCelani-at !

Thank you so much for the report!

I've reproduced it here – https://tsplay.dev/wEGEZm

To be able to fix it, we should define our PropertyKey version – https://devblogs.microsoft.com/typescript/announcing-typescript-2-9-2/#:~:text=type%20KeyofBase%20%3D%20keyof%20any%3B

It will look like:

type KeyofBase = keyof any;

It equals to string when you enable the option – https://tsplay.dev/mZb7Pm
Otherwise, it will return string | number | symbol – https://tsplay.dev/Ndd7kN

I will fix it in the nearest release

@MarcCelani-at can you please check the changes from #279? Does it fix your problem, right?

Included it in the next release – #280