0

Typescript does not generate an error when I assign a string value to a boolean variable.

I forced an error to see how the typescript behaved and it did not generate the error that I expected.

I created a class with an attribute of type boolean. Then I created an interface. And I created the constructor method to initialize the variable.So I get a json where the variable is of type string. To my surprise the boolean variable receives the string. I wish it would generate an error. My code does not respect the type of the variable.

export class Product{

    public status:boolean;

    constructor(obj?: IProduct) { 

        if(obj)
        {
            this.status = obj.status;
        }
    }

}

interface IProduct {
    status:boolean;
}

my json of server

{status: "test"}

The test value is assigned to the status variable. And this should not be allowed.

Does anyone know how I can ensure that the variable only receives boolean value?

2
  • nome or status?
    – Vega
    Jul 23, 2017 at 15:20
  • There is no "status" attribute in the interface. Jul 23, 2017 at 15:21

4 Answers 4

4

There's no type enforcement at runtime.

Decorators (an experimental feature) may solve your problem:

class Product {
    @validate
    public status: boolean;

    constructor(obj?: IProduct) { 
        if(obj) {
            this.status = obj.status;
        }
    }
}

interface IProduct {
    status: string;
}

function validate(target: any, propertyKey: string) : any {
    if (propertyKey === "status") {
        return {
            set: function (value: any) {
                if (typeof value !== "boolean") {
                    throw new Error("The supplied value is not of type boolean");
                }
            }
        }
    }
}
3
  • It seems like you're right. There appears to be no runtime type checking
    – Jobsdev
    Jul 23, 2017 at 16:08
  • The question I have is: porque usar tipagem if typescript does not handle type at runtime. Typescript does not take into account the types of data coming from the server
    – Jobsdev
    Jul 23, 2017 at 17:14
  • 1
    Runtime type enforcement is not in the TypeScript Design Goals. Have a look at this issue and the discussion.
    – Andreas
    Jul 23, 2017 at 21:57
1

It might have to do with you Interface

interface IProduto { // typo it should be IProduct
    nome:boolean; // and this should be name i suppose but i left it nome
}

And If you want to ensure that all values must have specified types you can set noImplicitAny to true in tsconfig.json,implies to TS Classes.

0
1

Try:

interface IProduct {
    status: Boolean;
}

notice the interface typo and change nome to status.

0
0

you can use this if statement in your constructor:

if (typeof obj !== 'boolean') {
    throw Error('type of obj must be boolean');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.