preactjs / preact

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
https://preactjs.com
MIT License
36.35k stars 1.93k forks source link

Type inference of Omit type not working, CSSProperties in preact/compat #4427

Closed MTtankkeo closed 5 days ago

MTtankkeo commented 5 days ago
import { CSSProperties } from "preact/compat";

interface ExampleProperties extends Omit<CSSProperties, "display"> {
    // ... skip propertys

    [key: string]: any;
}

This issue is not present in react, In the code above, the auto-completion function of Sample Properties (which seems to be a type inference problem) is not working.

Of course, if you don't use Omit, type inference of the CSSProperties is working.

rschristian commented 5 days ago

This is a TypeScript "issue" (technically working as intended, "limitation" might be a better word), not a Preact issue. See: https://github.com/microsoft/TypeScript/issues/49656. I don't think forming our types in precisely the same way to get around limitations in TS is really within the scope of compat. At that point you might be better off trying to use React's types.

However, you can use something like this instead:

type MyOmit<T, K extends PropertyKey> = { [P in keyof T as Exclude<P, K>]: T[P] }

interface ExampleProperties extends MyOmit<CSSProperties, 'display'> {
    [key: string]: any;
}
MTtankkeo commented 5 days ago

thanks, and that has helped me a lot.

rschristian commented 5 days ago

Oh, and as I forgot to link it, here's a StackOverflow post that I swiped that impl from with a bit more context: https://stackoverflow.com/a/76616671/8963648