-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Object.create return type #3865
Comments
Mostly we see
Can you post some examples of what this looks like? I can't think of any code that it would meaningfully benefit from a different declaration of |
And let foo = Object.create<FooLike>(Foo.prototype);
let foo = <FooLike> Object.create(Foo.prototype); |
@IMPinball Of course it isn't, but I don't really see what relevance that has here. Also, I don't think the former line in your block of code is a cast, but a type parameter. @RyanCavanaugh, here's an example: interface IHouse {
location: Coordindate;
price: number;
developerName: string;
}
var baseHouse = {
location: new Coordinate(50, 100),
price: 350000,
developerName: 'Test Developer'
};
var house1 = Object.create(baseHouse);
house1.price = 400000;
var house2 = Object.create(baseHouse);
house2.price = 360000;
// Assume the type of buildHouses is `(houses: IHouse[]) => void`
buildHouses([
house1,
house2
]); This code will work because |
@matthewjh I understand what you are saying, and from an overall logic perspective it makes sense, but what problem do you see with this? var house1 = <IHouse> Object.create(baseHouse);
house1.price = 400000;
var house2 = <IHouse> Object.create(baseHouse);
house2.price = 360000; If you made the change you suggested, everyone using |
That's a fair point, but the same could be said for many things:
Sure, we can use a cast based on what 'we' think the type is, but we shouldn't wherever possible. That's why we have a type inference system and type checking in the first place -- to cut humans out of figuring out types themselves and thereby making errors. The problem with casting the return value of Given
what happens if If the definition of
this would be easily prevented and picked up during type analysis without introducing any breaking changes (because T could be inferred). The problem is that when property descriptors are passed to Object.create the returned type will be a supertype of T, which is why my original proposal was However, that would be a breaking change as you point out (assuming TypeScript's type inference system isn't able to infer cases where
T could still be inferred in most (all?) cases, and the typing will still, I believe, be correct even if property descriptors are passed, because if you add properties to a I'm still new to TypeScript myself, so feel free to point out any errors I've made. :) (By the way, Kit, I recognised your name in the notification of your comment. I used to work at Sky!) |
Okay. I see what you mean WRT interfaces. Object.create doesn't change interfaces, but it doesn't work with class prototypes. |
And as for |
Oops... Ignore that comment. (that would require using only the interface, which TypeScript doesn't support.) |
Hi,
Just curious as to the reasoning behind
Object.create
having a return value of typeany
in lib.d.ts.Wouldn't this make more sense?
create<T extends A>(o: A, properties?: PropertyDescriptorMap): T;
since the returned object is guaranteed to have the properties of
o
through prototypal inheritance.At present I'd like to create many objects (via literals) of type
X
, but given that many of the properties are shared, usingObject.create
makes a lot of sense. It seems a shame that I must use this at the expense of typing.The text was updated successfully, but these errors were encountered: