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

RangeError: Maximum call stack size exceeded #9390

Closed
benjamin-hg opened this issue Jun 28, 2016 · 5 comments
Closed

RangeError: Maximum call stack size exceeded #9390

benjamin-hg opened this issue Jun 28, 2016 · 5 comments
Assignees
Labels
Bug A bug in TypeScript
Milestone

Comments

@benjamin-hg
Copy link

While playing around with generic type constraints, I came across a bug in the typescriptServices.js used on the ts playground:

TypeScript Version: 1.8.2 (66d9c92bd3d6127ed3142876bc1a446bbd8395de)

Code

function cast<TSparse extends TFull|TSparse, TFull extends TFull & TSparse>
(input:TSparse):TFull{
    return input; 
} 

Expected behavior:
A (meaningful) compile error or compiled code or both

Actual behavior:
RangeError: "Maximum call stack size exceeded" and no compile result in TS Playground with this code.

@benjamin-hg
Copy link
Author

I know it's an exotic kind of type constraint usage and I'm not very surprised it didn't work. I just reported it because the compiler probably is not supposed to crash.

@benjamin-hg
Copy link
Author

BTW: what I was trying to do:

Problem:
I have an interface Item that has a lot of non-optional properties. In some unit tests, I need to create objects of that interface, but do not need all these properties.

I want to be able to define a "sparse" item with only some of the otherwise mandatory properties present.

My approach:
I created an interface SparseItem that has the same properties, but all are optional. I use it for the variable that holds my test dummy.

When I want to pass it to methods that expect an object of interface Item, I have to do a kind of cast.

var item = <Item><any>sparseItem;

would have done the job, but not very safely. So I thought I could wirte a cast function that would do a better job. Usage like this:

var item:Item = cast<SparseItem, Item>(sparseItem);

My goal:

  • When a property of Item is removed or renamed, but not on SparseItem and it is still used, I get an error.
  • (Optional) When a property is added to Item, but not on SparseItem, I get an error.

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 28, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Jun 28, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 28, 2016

This should be flagged as an error by the compiler. a type parameter can not depend on itself, this is not supported.
I think what you are looking for is something like a type operator that gives you an all optional version of a type. this is tracked by #7004

@benjamin-hg
Copy link
Author

@mhegazy yes, exactly -- thanks for pointing me to #7004

@mhegazy mhegazy modified the milestones: Future, TypeScript 2.1 Sep 29, 2016
@sandersn
Copy link
Member

sandersn commented Jan 7, 2020

Fixed in 3.8

@sandersn sandersn closed this as completed Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants