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

Feature Request: dynamic_cast i.e. optional runtime structural type checks #5896

Closed
RaduW opened this issue Dec 2, 2015 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@RaduW
Copy link

RaduW commented Dec 2, 2015

Motivation

Verify inputs at application boundary.

When a Typescript application interacts with an external system (e.g. receives a Json object from a http request, or receives an object from an external library) a typical approach is to use the cast operator for the input and to assume/hope that the application receives the required type.

If/when the external system changes the assumption can be broken resulting in sometimes hard to find bugs and unhelpful runtime errors like "undefined is not a function".

These bugs are also quite hard to catch in tests since unit tests isolate the external system, only integration tests would be able to catch these scenarios.

Proposal

A new cast operator could be introduced that does structural type checking at runtime.
Example:

$http.get('https://mySite.com/products', ( result: { data: IProduct[] } ) =>{
    var data:IProduct[] = dynamic_cast<IProduct[]>result.data;
});

The Typescript compiler could operate in 2 modes ( debug / release or runtime type checks on/off)

In release the compiler would simply remove the dynamic_cast operator.
In debug mode the compiler would wrap the expression in a typeCheck call. It would generate something like this:

$http.get('https://mySite.com/products', function( result ) {
    var data:IProduct[] = IProduct.CheckType(result.data);
});

The tricky bit comes in implementing the CheckType() function.

Interfaces that are dynamically checked would suddenly acquire a runtime footprint.

Also any interface and Type that appear as the type of members of any interface and type that are type checked would also need to receive a ChekType static member function.

It would be still nicer if it would be possible to support a syntax like:

$http.get('https://mySite.com/products', ( result: dynamic_cast<{ data: IProduct[] }> ) =>{
    var data:IProduct[] = result.data;
});

Although it is not very clear (to me) where would the cast function be scoped and how it would be called, probably the lambda would be wrapped in something ugly like:

$http.get('https://mySite.com/products', 
    function (result){
        return (function( result ) {
            var data:IProduct[] = IProduct.CheckType(result.data);
        })( ???.CheckType(result));
});
@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2015

looks like a duplicate of #1549

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 2, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants