Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Simplify handle all types #414

Merged
merged 10 commits into from Jul 2, 2022
2 changes: 1 addition & 1 deletion source/simplify.d.ts
Expand Up @@ -55,4 +55,4 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`

@category Object
*/
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]};
export type Simplify<T> = T extends Function ? T : {[KeyType in keyof T]: T[KeyType]};
22 changes: 22 additions & 0 deletions test-d/simplify.ts
Expand Up @@ -42,3 +42,25 @@ expectType<SomeInterfaceAsTypeWrittenByHand>(valueAsInterface);
expectAssignable<Record<string, unknown>>(valueAsLiteral);
expectAssignable<Record<string, unknown>>(valueAsSimplifiedInterface);
expectNotAssignable<Record<string, unknown>>(valueAsInterface); // Index signature is missing in interface

// Should return the original type if it is not simplifiable, like a function.
type SomeFunction = (type: string) => string;
expectType<Simplify<SomeFunction>>((type: string) => type);

// Sould simplify to public interface (what it is currently doing)? Return the Class type? Or something else?
skarab42 marked this conversation as resolved.
Show resolved Hide resolved
class SomeClass {
id: string;

private readonly code: number;

constructor() {
this.id = 'some-class';
this.code = 42;
}

someMethod() {
return this.code;
}
}

expectType<Simplify<SomeClass>>({id: 'prout', someMethod: () => 42});