Why is the code compiling successfully? Since barit is not part MyState, I expect it to generate a compiler error.
bar
MyState
type MyState = { foo: number; }; type Reducer<T> = (state: T) => T; const wtf: Reducer<MyState> = (state) => { return { foo: 123, bar: 123 }; // `bar` isn't part of MyState };
Type Compatibility in TypeScript is based on structural subtyping. Structural typing is a way of binding types based solely on their members. This contrasts with typical typing.
Type compatibility
, y x, x, y. y , , . , . : y location, . ( ).
, { foo: 123, bar: 123 } foo, , bar .
{ foo: 123, bar: 123 }
foo
: . ? ?
crashmstr , , , .
.
:
var x: MyState = { foo: 10, bar: 20 };
{foo: 10, bar: 20}
10
number
. , . ,
// Error var x: MyState = { foo: 10, bar: 20 };
// OK var x1 = { foo: 10, bar: 20 }; var x2: MyState = x1;
, x1.
x1
- , . , , .
Source: https://habr.com/ru/post/1684404/More articles:Centering without flexbox - htmlIs it possible to disable caching for some kafka themes? - apache-kafkaJavascript - sort an array of strings in your own way - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1684402/can-i-write-a-c-program-that-when-run-as-a-scheduled-task-detects-when-task-scheduler-tries-to-stop-it&usg=ALkJrhhJp8Mg15NdesW9qaHQgeFECF6_YgImgur API GET image problems - jqueryReplication asynchronously via github API to get files - javascriptDo we really need std :: error_category and std :: error_condition? - c ++Deploying an Azure web application through ARM / msdeploy without deleting existing files - azureHow to disconnect from google authentication after angularfire2 / auth signOut? - angularDoes the CSS Grid have a flexible growth feature? - htmlAll Articles