How excess property check helps?
up vote
1
down vote
favorite
For the below code,
interface SquareConfig{
color?: string;
width?: number;
}
interface Square{
color: string;
area: number;
}
function createSquare(config: SquareConfig): Square {
let newSquare:Square = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
below argument(myObj
) inferred as type any
is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.
let myObj = {colour: 'red', width: 100};
let mySquare = createSquare(myObj);
In second case, below argument(other thanSquareConfig
type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.
let mySquare = createSquare({colour: 'red', width: 100});
What is the purpose of excess property check, in second case?
typescript ecmascript-6 duck-typing
add a comment |
up vote
1
down vote
favorite
For the below code,
interface SquareConfig{
color?: string;
width?: number;
}
interface Square{
color: string;
area: number;
}
function createSquare(config: SquareConfig): Square {
let newSquare:Square = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
below argument(myObj
) inferred as type any
is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.
let myObj = {colour: 'red', width: 100};
let mySquare = createSquare(myObj);
In second case, below argument(other thanSquareConfig
type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.
let mySquare = createSquare({colour: 'red', width: 100});
What is the purpose of excess property check, in second case?
typescript ecmascript-6 duck-typing
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
For the below code,
interface SquareConfig{
color?: string;
width?: number;
}
interface Square{
color: string;
area: number;
}
function createSquare(config: SquareConfig): Square {
let newSquare:Square = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
below argument(myObj
) inferred as type any
is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.
let myObj = {colour: 'red', width: 100};
let mySquare = createSquare(myObj);
In second case, below argument(other thanSquareConfig
type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.
let mySquare = createSquare({colour: 'red', width: 100});
What is the purpose of excess property check, in second case?
typescript ecmascript-6 duck-typing
For the below code,
interface SquareConfig{
color?: string;
width?: number;
}
interface Square{
color: string;
area: number;
}
function createSquare(config: SquareConfig): Square {
let newSquare:Square = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
below argument(myObj
) inferred as type any
is allowed to pass as argument by type checker at compile time. JS code use duck typing at runtime.
let myObj = {colour: 'red', width: 100};
let mySquare = createSquare(myObj);
In second case, below argument(other thanSquareConfig
type) is not allowed to pass by type checker at compile time. As mentioned in handbook: Object literals get special treatment and undergo excess property checking when assigning them to other variables, or passing them as arguments.
let mySquare = createSquare({colour: 'red', width: 100});
What is the purpose of excess property check, in second case?
typescript ecmascript-6 duck-typing
typescript ecmascript-6 duck-typing
edited May 2 at 20:56
asked May 2 at 20:41
overexchange
3,49462571
3,49462571
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
What is the purpose of excess property check, in second case?
It correctly detects bugs (as shown in this case, the misspelling of color
) without creating too many false positives.
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj
- we may be inspecting it only for its .width
here but then using its .colour
in some other place.
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of typeany
.
– Ryan Cavanaugh
May 3 at 8:57
colour
property inmyObj
makes it of typeany
. Isn't it?
– overexchange
May 3 at 21:27
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
What is the purpose of excess property check, in second case?
It correctly detects bugs (as shown in this case, the misspelling of color
) without creating too many false positives.
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj
- we may be inspecting it only for its .width
here but then using its .colour
in some other place.
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of typeany
.
– Ryan Cavanaugh
May 3 at 8:57
colour
property inmyObj
makes it of typeany
. Isn't it?
– overexchange
May 3 at 21:27
|
show 2 more comments
up vote
4
down vote
accepted
What is the purpose of excess property check, in second case?
It correctly detects bugs (as shown in this case, the misspelling of color
) without creating too many false positives.
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj
- we may be inspecting it only for its .width
here but then using its .colour
in some other place.
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of typeany
.
– Ryan Cavanaugh
May 3 at 8:57
colour
property inmyObj
makes it of typeany
. Isn't it?
– overexchange
May 3 at 21:27
|
show 2 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
What is the purpose of excess property check, in second case?
It correctly detects bugs (as shown in this case, the misspelling of color
) without creating too many false positives.
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj
- we may be inspecting it only for its .width
here but then using its .colour
in some other place.
What is the purpose of excess property check, in second case?
It correctly detects bugs (as shown in this case, the misspelling of color
) without creating too many false positives.
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj
- we may be inspecting it only for its .width
here but then using its .colour
in some other place.
answered May 2 at 22:07
Ryan Cavanaugh
92.4k27159170
92.4k27159170
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of typeany
.
– Ryan Cavanaugh
May 3 at 8:57
colour
property inmyObj
makes it of typeany
. Isn't it?
– overexchange
May 3 at 21:27
|
show 2 more comments
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of typeany
.
– Ryan Cavanaugh
May 3 at 8:57
colour
property inmyObj
makes it of typeany
. Isn't it?
– overexchange
May 3 at 21:27
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
It is weird that object literal has excess property check, but not object myObj. Why is that?
– overexchange
May 3 at 1:18
1
1
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
Because the object isn't aliased anywhere else, TypeScript can be fairly confident that the excess property isn't going to be used for a different purpose in some other part of the code. The same cannot be said of myObj - we may be inspecting it only for its .width here but then using its .colour in some other place.
– Ryan Cavanaugh
May 3 at 5:21
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
In case of myObj, you know at compile time that it is of type any, then how would you allow it to be passed to function which accepts SquareConfig type?
– overexchange
May 3 at 8:25
myObj
is not of type any
.– Ryan Cavanaugh
May 3 at 8:57
myObj
is not of type any
.– Ryan Cavanaugh
May 3 at 8:57
colour
property in myObj
makes it of type any
. Isn't it?– overexchange
May 3 at 21:27
colour
property in myObj
makes it of type any
. Isn't it?– overexchange
May 3 at 21:27
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50143250%2fhow-excess-property-check-helps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown