Type : Extract only properties from class including any

I’m trying to extract properties from class to create a type.

ts-essential comes in very handy with OmitProperties !

Only my problem is OmitProperties<T, Function> will not only remove the class methods but also every property that is of type any.

type GetProperties<T> = OmitProperties<T, Function>

class Foo {
    foo: string = '';
    bar: any | null = null;
}

export type FooProperties = GetProperties<Foo>; //  only { foo: string; } =(

Any idea how to improve this to include every properties, including the ones typed as any ?

Playground

>Solution :

Use unknown instead of any (actually, this is applicable to almost any case of using any):

class Foo {
    foo: string = '';
    bar: unknown | null = null;
}

export type FooProperties = GetProperties<Foo>; //  only { foo: string; } =(

// type FooProperties = {
//    foo: string;
//    bar: unknown | null;
// }

Leave a Reply