You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to perform a destructuring assignment in a method signature using a variable that is declared as an interface that has an optional property. I'm getting an error when specifying multiple levels of defaults.
Please take a look at the following code. I expected test number 3 to compile cleanly, but it gives an error. Tests 2 and 5 give what I think is an appropriate error. The rest compile cleanly.
For test 3, since a default is provided for B, I don't think it should be considered mandatory.
This is with TypeScript 1.6 beta. Please let me know if I'm doing something wrong.
Thanks, team!
-Steve O
interfaceAB{A: string;B?: string;}constchildObject: AB={A: "test"}constparentObject={
childObject
};functionfunction1({childObject: {A, B =""}}){console.log(A);console.log(B);}functionfunction2({childObject: {A, B =""}={A: "",B: ""}}){console.log(A);console.log(B);}functionfunction3({childObject: {A, B}={A: "",B: ""}}){console.log(A);console.log(B);}/* Test #1: Compiles cleanly */function1(parentObject);/* Test #2: Gives expected error. Error: Argument of type '{}' is not assignable to parameter of type '{ childObject: { A: any; B?: string; }; }'. Property 'childObject' is missing in type '{}'.*/function1({});/* Test #3: Not sure why this one is giving an error. A default is provided for B if the object exists, so technically B is not required. I would have only expected to see this error on Test #5 below. Argument of type '{ childObject: AB; }' is not assignable to parameter of type '{ childObject?: { A: string; B: string; }; }'. Types of property 'childObject' are incompatible. Type 'AB' is not assignable to type '{ A: string; B: string; }'. Property 'B' is optional in type 'AB' but required in type '{ A: string; B: string; }'.*/function2(parentObject);/* Test #4: Compiles cleanly */function2({});/* Test #5: Gives expected error. (Same as #3 but this one makes sense.) Argument of type '{ childObject: AB; }' is not assignable to parameter of type '{ childObject?: { A: string; B: string; }; }'. Types of property 'childObject' are incompatible. Type 'AB' is not assignable to type '{ A: string; B: string; }'. Property 'B' is optional in type 'AB' but required in type '{ A: string; B: string; }'.*/function3(parentObject);/* Test #6: Compiles cleanly */function3({});
The text was updated successfully, but these errors were encountered:
By the way - using this code on babeljs.io logs "test" and "b1" which is what I would have expected from test number 3 above. Also, the TypeScript code works as expected - it's just the type error that I'm concerned with.
functionfunction2({childObject: {A, B ="b1"}={A: "",B: "b2"}}){console.log(A);console.log(B);}constchildObject={A: "test"}constparentObject={
childObject
};function2(parentObject);
Hi,
I am trying to perform a destructuring assignment in a method signature using a variable that is declared as an interface that has an optional property. I'm getting an error when specifying multiple levels of defaults.
Please take a look at the following code. I expected test number 3 to compile cleanly, but it gives an error. Tests 2 and 5 give what I think is an appropriate error. The rest compile cleanly.
For test 3, since a default is provided for B, I don't think it should be considered mandatory.
This is with TypeScript 1.6 beta. Please let me know if I'm doing something wrong.
Thanks, team!
-Steve O
The text was updated successfully, but these errors were encountered: