6 ms·
I might be missing something, but isn't this line backwards? > Subtype of type A is a subset of type A. Supertype is a superset. Easy. Subtype of type A is ac
by jakelazaroff 4y ago
I might be missing something, but isn't this line backwards?
> Subtype of type A is a subset of type A. Supertype is a superset. Easy.
Subtype of type A is actually a superset of type A, since it contains at least all the properties of A.
If you had (contrived example) a Dog class that inherited from an Animal class, Dog would be a subtype of Animal, but its additional properties (say, a bark() method) mean that it actually has a superset of the properties in Animal. And vice versa: Animal is a supertype of Dog, but it's a subset because it contains only the properties that Dog inherits.
- epolanski 4y ago> Subtype of type A is a subset of type A. Supertype is a superset. Easy. I think it is correct. Example: type A = { a: number } type B = { a: number, b: number } B is not a superset of A, it's a subset and subtype of A. You can use B every time you want to use A. In other words, of all types that can be assigned to A or used in functions and types expecting an A, B is one of those subsets. type C = { a: number, c: number } is another subtype of A. A "subtype" C can be substituted in place of its "supertype" A. Both C and B are subsets of A, in other words: in the infinite set of values of type A, some can be grouped in a type B or C. C itself is a subtype of A.
- thoughtspile 4y agoYou're making the same mistake as I sometimes do, thinking in terms of "object shape" or "functionality". Here, I use "subset" in a sense of "all values that belong to Sub also belong to Super", in line with a set-theoretic view of types in the post. Also see subsets on wiki: https://en.wikipedia.org/wiki/Subset https://en.wikipedia.org/wiki/Subset Try this one: A is subset of B iff all the values that belong to A also belong to B. In your example (and in real life), every dog is an animal, so dogs are a subset of animals.
- hbrn 4y agoTypes are not features, they are constraints. "at least all the properties of A" means "all of the constraints of A and some more". Subtype of A is more narrow than A, even if it means that object of subtype A has to have more fields.
- feoren 4y agoIn your case, dogs are only those animals that bark. That's a subset of animal. Think of it this way: can you think of an object that satisfies (is-a) Dog, but not Animal? No. Can you think of an object that satisfies Animal, but not Dog? Yes. So Dog ⊂ Animal. hbrn has it right calling those properties constraints, not features. With anything but a sealed class, the properties that are not present are unspecified rather than missing. And indeed sealed classes feel very "un-set-theoretic" to me.
- RyanCavanaugh 4y agoNope. This is a common confusion. The set of properties of objects and the sets of objects themselves have a complementary relationship when it comes to union / intersection and subset / superset. Let's define a "property" as being a predicate that is true for all elements of a set. For example, a collection of red objects has the "red" property. A subset of a set of objects can only have the same or more properties than the set you started with. If you take a bunch of marbles of varying colors, selecting a subset of those marbles can yield a set with a new property (such as them all being red), but they are guaranteed to have the "marble" property. A superset of objects can only have the same or fewer properties than the set you started with. Adding marbles to an existing set of marbles can only make "they are all red" become false (if it was true to begin with). Union and intersection have the same duality; the union of two sets has the intersection of its properties, and the intersection of two sets has the union of its properties. These results come from logic, not TypeScript.
- yitr 4y agomaybe a dumb question, but why does wikipedia say typescript is a superset of javascript? https://en.wikipedia.org/wiki/TypeScript https://en.wikipedia.org/wiki/TypeScript
- thoughtspile 4y agoBecause all valid JS code is also valid TS code (considering implicit any). Or, put another way, the set of all valid JS programs is a subset of all valid TS programs.
- sirmarksalot 4y agoBecause Typescript is designed to accept any valid Javascript program, the set of valid statements in a Typescript program is a superset of the set of valid statements in a Javascript program. Typescript contains all the rules of Javascript, and then adds some more, but never in a way that contradicts the requirement that a plain Javascript program should compile, so that also means the language specification itself is a strict superset of Javascript.
- Koshkin 4y ago> 4. A extends B ... can be read as "A is subset of B". I agree, it is unnatural to think of a subset as "extending" the set it is part of.
- Jtsummers 4y agoYeah, it's a common way of telling people how to read "A extends B", but it's not quite right. The two relationships are related (no pun intended) but they aren't the same relationship. `A extends B` means (in the OO world) that A adds fields, methods, or changes the behavior of methods to B. `A is a subset of B` means that all As are Bs, but some Bs may not be As. It happens that `A extends B` introduces a subset relation between them, but it's not correct to describe it as the same as the subset relation.
- jotaen 4y agoI think what’s confusing here is that there are two mental models that use the word “type” in an opposing way: if you think of types in terms of set theory, then “subtype of type A is a subset of type A”. But if you think of types in terms of a class hierarchy (like in Java), then by saying “subtype of type A is a superset of type A” you actually mean “an inherited type (class) that is further down in the class hierarchy has the same or more properties than the parent type (class) that is further up”. So I actually would agree that your original statement is valid, assuming that you refer to “types” as “classes” in the sense of Java class hierarchies.