Skip to content

Commit

Permalink
fix: interfaces and objects are not treated equally, WIP #3
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreytools committed Feb 20, 2023
1 parent 8b8b03c commit 7a90048
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,19 @@ type _Disambiguate<T, Model> =
? T extends [] | [unknown, ...unknown[]]
? DisambiguateTuple<T, Model>
: Disambiguate<T[0], 0 extends keyof Model ? Model[0] : never>[]
: T extends { [k: PropertyKey]: unknown }
? { [K in keyof T]: Disambiguate<T[K], K extends keyof Model ? Model[K] : never> }
& { readonlyKeys: inferReadonlyKeys<T> }
& Fork<Never<Model>, {}, OptionalProps<Model>>
: T extends { [k: PropertyKey]: unknown } ? DisambiguateObject<T, Model>
: T extends number | string | boolean | bigint ? DisambiguateBranded<T>
: DisambiguateOther<T>
: DisambiguateOther<T, Model>

type DisambiguateTuple<T, Model> = {
[K in keyof T]: K extends keyof [] ? T[K] : Disambiguate<T[K], K extends keyof Model ? Model[K] : never>
};

type DisambiguateObject<T, Model> =
{ [K in keyof T]: Disambiguate<T[K], K extends keyof Model ? Model[K] : never> }
& { readonlyKeys: inferReadonlyKeys<T> }
& Fork<Never<Model>, {}, OptionalProps<Model>>

type Intrinsic =
| Uppercase<string>
| Lowercase<string>
Expand All @@ -212,10 +214,14 @@ type _IsIntrinsic<T> = boolean extends (
) ? true : false;

type DisambiguateOther<
T,
T, Model,
U extends Unwrapped = unwrap<T, TypesMap>,
> = [U] extends [never] ? T
: apply<U['type'], DisambiguateTuple<U['args'], never>>;
> = ([U] extends [never] ? true : false) extends false
? apply<U['type'], DisambiguateTuple<U['args'], never>>
: T extends Interface ? DisambiguateObject<T, Model>
: T;

type Interface = { [k: string]: any } & { [Symbol.toStringTag]?: never }

type DisambiguateBranded<T, Tag = GetTag<T, Widen<T>>> =
Tag extends object ? GetRadical<T, Tag> & Disambiguate<Tag, never> : Tag
Expand Down
17 changes: 17 additions & 0 deletions test/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test, _ } from '../src'

type Foo = { helloWorld: string };

interface Bar {
helloWorld: string;
}

interface Qux {
helloWorld: any;
}

test("interfaces and objects are treated equally", t => [
t.equal<Foo, Bar>(),
t.not.equal<Foo, Qux>(),
t.not.equal<Bar, Qux>()
]);

0 comments on commit 7a90048

Please sign in to comment.