Is this really not so?
class Animal { } class Person { } type MyUnion = Number | Person; var list: Array<MyUnion> = [ "aaa", 2, new Animal() ]; // Shouldn't this fail? var x: MyUnion = "jjj"; // Shouldn't this fail?
Is there any way to force type control in this case?
TypeScript handles type-based compatibility structural subtyping.
structural subtyping
Structural typing is a way of binding types based solely on their members.
In particular, for classes:
When comparing two objects of a class type, only instance instances are compared. Static elements and constructors do not affect compatibility.
Read more at https://www.typescriptlang.org/docs/handbook/type-compatibility.html#classes
, Animal Person -:
Animal
Person
class Animal { name: string; } class Person { age: Number; } type MyUnion = Number | Person; var list: Array<MyUnion> = [ "aaa", 2, new Animal() ]; // Fails now var x: MyUnion = "jjj"; // Fails now
Animal Person, ( -, ) , .
Source: https://habr.com/ru/post/1665810/More articles:Vue.js: dynamic computed subcomponents - javascriptPhusion Passenger and Rails app using NVM Could not find JavaScript runtime - javascriptadditional vars files with space in value - ansibleHow to measure the popularity of a language through Github Archive data? - githubSaving the regression residuals in the original data frame - rIs there a method in Java that mimics a cast to a character? - javaCombining dplyr :: do () with dplyr :: mutate? - rChanging the li reordering list - listReal World Controller Example with Spring 5: Web Reactive - javaHow to implement password expiration and passwords for archiving using amazon cognito - ruby | fooobar.comAll Articles