-
Notifications
You must be signed in to change notification settings - Fork 636
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
collections: most functions that work on Record
do not work with interface
#1126
Comments
Record
do not work with interface
I think we could fix this with Partial Records. I will take a look at fixing it. |
Having read into this, I realized that have been down that rabbit hole before. I do not think there is an easy fix for this without breaking the type promise of the functions. The problem is that Given interface Human { age: number }
interface Developer extends Human { usesVim: boolean } you would expect that passing a This is generally one of the many reasons you should use type aliases over interfaces by default if possible, as they provide stronger guarantees because they can neither be extended nor added onto by arbitrary libraries. There are hacks to pass this - the least ugly of them being spread ( I do not see a fix for this if we want type signatures to stay helpful. |
I agree with most, however 2 small points:
There isn't an elegant solution, but we can try to augment the type of functions that one might use on interfaces to help our users. |
@zhangyuannie Something that can help considerably with this issue is to use the following type: type Typify<T> = { [ K in keyof T ]: T[ K ] }; Example : import { deepMerge, filterEntries } from "https://deno.land/std/collections/mod.ts";
interface Foo {
bar: string;
}
type Typify<T> = { [ K in keyof T ]: T[ K ] };
const foo1: Typify<Foo> = { bar: "deno" };
const foo2: Typify<Foo> = { bar: "deno" };
const a = deepMerge(foo1, foo2);
const b = filterEntries(foo1, ([key]) => key !== "a"); |
That is definitely debatable :-) I do not think there is a reasonable solution on the API side though, so I would vote to close this issue. |
I think this issue should probably be closed, though the discussion in this issue is very interesting. |
Interfaces tend to be used for objects with well-defined shapes, while records tend to be used for objects that are more generic, which aligns with the |
Most of
collections
' functions that work onRecord
do not work withinterface
To Reproduce
Expected behavior
It compiles.
Current behaviour
It does not compile.
Desktop (please complete the following information):
Additional context
Possible workaround is to use type instead of interface, but that's not ideal:
The text was updated successfully, but these errors were encountered: