1

Not Duplicate

This is not the duplicate question because here my issue is in index signature not in object literal or type annotation. Please read carefully my question.

In few days I started to learn TypeScript. While when I reached at topic duck typing. I am confused in some of its basic rules.

Overview

In below code I have complete understanding. That, why we are using duck typing.

//Object declaration & initialization
let obj1 : {id:number, name:string};
obj1 = {id: 1,name: "Ahmer Ali Ahsan"};
//Error, missing property name
obj1 = {id:1};

let obj2 : {id:number, [obj2: string]:any};
obj2 = {id: 1, firstname: "Ahmer"};
//We add new property members due to index signatures
obj2 = {id: 2, firstname: "Ahmer", middlename:"Ali", lastname: "Ahsan"};
//We erase property members due to index signatures
obj2 = {id:2};

Problem

I have a question that if TypeScript is a type safety language. Then in below code I have all parameter types are number and index signature type is also a number.

let obj3 : {id:number, [obj3: number]: number};
obj3 = {id: 1, rollno: "Why error is not showing here?"};

Question

Why is there no error due to the [obj: number]: number index signature when I assign rollno to a string value?

Please explain briefly with your answers. If my concepts of index signature is still not clear.

2
  • Possible duplicate of Type definition in object literal in TypeScript Commented Jun 28, 2016 at 5:20
  • @Sheepy this is not a duplicate question because I am not taking about type annotation or object literal. My issue is related to index signature. Please ready carefully my question and complete detail of it. Thanks Commented Jun 28, 2016 at 5:36

1 Answer 1

3

Your interpretation: All keys of this object must be numeric, and all values must be of type number

Actual interpretation: All values reached by numeric keys must be of type number

Consider Array<boolean>. It has a numeric index signature to type boolean because e.g. accessing arr[0] produces a value of type boolean. But arr.toString (a key which is a string type) has type () => string, which is clearly not the same as boolean.


Edit to add based on comments.

Let's consider a declaration:

var x: { [n: number]: boolean; };

This is a numeric index signature. A numeric index signature says that properties with numeric names match a particular type. They have no effect on properties with non-numeric names.

Numeric index signatures only constrain properties with numeric names (not string names)

Numeric index signatures only constrain properties with numeric names (not string names)

Numeric index signatures only constrain properties with numeric names (not string names)

In other words, these are OK, because their key names are not numeric:

x = { hello: 100 }; // OK! 'hello' is not numeric
x = { rollno: 'wat!'}; // OK! 'rollno' is not numeric
x = { lookThisIsNotNumeric: false }; // OK! 'lookThisIsNotNumeric' is not numeric

Conversely, these are not OK, because the numeric-named properties do not fit the index signature type:

x = { 1: 'hello' }; // Error! 1 is a number and 'hello' is not a boolean
x = { 10: 653 }; // Error! 10 is a number and 653 is not a boolean
x = { 25: 'hello' }; // Error! 25 is a number and 'hello' is not a boolean

Finally, this is OK, because the numeric-named property has a boolean value:

x = { 42: true };
Sign up to request clarification or add additional context in comments.

7 Comments

I appreciate your answer. But its easy to me for understand. If you reference my index signature example in your answer.
In my example parameter rollno has type number but in below line I put string instead of number. Why its not showing me an error?
rollno is not a number, it's a string. string-keyed values are not subject to the constraints of the numeric index signature because you can't seen through a numeric key.
In other words, if a sign on a door said "All people wearing hats must be wearing shoes" and someone came through the door with no hat and no shoes, that's not a violation of the sign. Same here, the rule implied by the index signature is "All properties with numeric keys must be numbers".
I understand your last comment but not understand your second last comment. If my rollno is not a number then in my index signature why I am using type number [obj3: number]. Please clear my miss understanding.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.