Skip to content

Convert type alias to interface? #4

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

Open
alloy opened this issue Dec 4, 2020 · 1 comment
Open

Convert type alias to interface? #4

alloy opened this issue Dec 4, 2020 · 1 comment
Labels
question Further information is requested type projection All about Flow to TypeScript projections
Milestone

Comments

@alloy
Copy link
Member

alloy commented Dec 4, 2020

TypeScript allows for declaration merging of interfaces, which appears to be something that people do to extend types for react-native-web, whereas this cannot be done with type aliases. So it might be preferable to convert to interfaces.

interface X { foo: string }
interface X { bar: string } // merge declaration
const x: X = { foo: "foo", bar: "bar" }

In case it makes things easier, perhaps as a first step, we could also create a temp type alias and use it to extend an empty interface with:

type $1 = { foo: string }
interface X extends $1 {}
interface X { bar: string } // merge declaration
const x: X = { foo: "foo", bar: "bar" }
@alloy alloy added question Further information is requested type projection All about Flow to TypeScript projections labels Dec 4, 2020
@alloy
Copy link
Member Author

alloy commented Dec 16, 2020

This might be something we just do selectively using overrides. Cases like RN’s ViewProps definitely make sense from an error reporting perspective too. Errors where type aliases are involved show the full typing, whereas an interface shows only an interned name.

microsoft/TypeScript#13095

E.g.

Type '(Readonly<Readonly<{ onBlur?: ((event: Readonly<{ bubbles?: boolean | null | undefined; cancelable?: boolean | null | undefined; currentTarget: unknown; defaultPrevented?: boolean | null | undefined; dispatchConfig: Readonly<...>; ... 10 more ...; type?: string | ... 1 more ... | undefined; }>) => unknown) | null | ...' does not satisfy the constraint '{ readonly [$f2tKey: string]: unknown; }'.
  Type 'Readonly<Readonly<{ onBlur?: ((event: Readonly<{ bubbles?: boolean | null | undefined; cancelable?: boolean | null | undefined; currentTarget: unknown; defaultPrevented?: boolean | null | undefined; dispatchConfig: Readonly<...>; ... 10 more ...; type?: string | ... 1 more ... | undefined; }>) => unknown) | null | u...' is not assignable to type '{ readonly [$f2tKey: string]: unknown; }'.
    Index signature is missing in type 'Readonly<Readonly<{ onBlur?: ((event: Readonly<{ bubbles?: boolean | null | undefined; cancelable?: boolean | null | undefined; currentTarget: unknown; defaultPrevented?: boolean | null | undefined; dispatchConfig: Readonly<...>; ... 10 more ...; type?: string | ... 1 more ... | undefined; }>) => unknown) | null | u...'.

VS

Type '(ViewProps & { children?: ReactNode; } & RefAttributes<Readonly<NativeMethods>>) | (ViewProps & RefAttributes<...>)' does not satisfy the constraint '{ readonly [$f2tKey: string]: unknown; }'.
  Type 'ViewProps & { children?: ReactNode; } & RefAttributes<Readonly<NativeMethods>>' is not assignable to type '{ readonly [$f2tKey: string]: unknown; }'.
    Index signature is missing in type 'ViewProps & { children?: ReactNode; } & RefAttributes<Readonly<NativeMethods>>'.

@alloy alloy added this to the v1 milestone Jan 5, 2021
@ZihanChen-MSFT ZihanChen-MSFT modified the milestones: v1, v2 Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested type projection All about Flow to TypeScript projections
Projects
None yet
Development

No branches or pull requests

2 participants