What does 'extends' actually mean in typescript?What is TypeScript and why would I use it in place of JavaScript?get and set in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?TypeScript Converting a String to a numberhow to create typescript definitions for function that extend objectsTypescript: Interfaces vs TypesTypeScript String Union to String ArraySyntax error for Typescript arrow functions with genericsInitializing union type in typescriptExtend a class within a function and keep typings
How to find location on Cambridge-Mildenhall railway that still has tracks/rails?
Software need db owner permission to master database (sql2016)
Pauli exclusion principle - black holes
How fast does a character need to move to be effectively invisible?
Difference between c++14 and c++17 using: `*p++ = *p`
Why did my "seldom" get corrected?
Why aren't there any women super GMs?
What makes MOVEQ quicker than a normal MOVE in 68000 assembly?
How can electric field be defined as force per charge, if the charge makes its own, singular electric field?
What are the basics of commands in Minecraft Java Edition?
Why isn't a binary file shown as 0s and 1s?
How would you say "Sorry, that was a mistake on my part"?
The most secure way to handle someone forgetting to verify their account?
Is this Android phone Android 9.0 or Android 6.0?
Arithmetics in LuaLaTeX
Strategy to pay off revolving debt while building reserve savings fund?
Whipping heavy cream with melted chocolate
Inscriptio Labyrinthica
Why don't humans perceive waves as twice the frequency they are?
Why were these characters absent in Spider-Man: Far From Home?
How do you give a date interval with diffuse dates?
Who determines when road center lines are solid or dashed?
How to belay quickly ascending top-rope climbers?
Practical example in using (homotopy) type theory
What does 'extends' actually mean in typescript?
What is TypeScript and why would I use it in place of JavaScript?get and set in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?TypeScript Converting a String to a numberhow to create typescript definitions for function that extend objectsTypescript: Interfaces vs TypesTypeScript String Union to String ArraySyntax error for Typescript arrow functions with genericsInitializing union type in typescriptExtend a class within a function and keep typings
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am not able to fully grasp the use of extends keyword in case of union types.
Here is a code snippet explaining the confusion that I have.
class SomeClass
someClassProp: string;
;
class SomeExtendedClass extends SomeClass
someExtendedClassProp: string;
;
function someClassFunction<T extends SomeClass>(args: T): T
return args;
;
let someClass: SomeClass, someExtendedClass: SomeExtendedClass;
someClassFunction(someClass);
someClassFunction(someExtendedClass); // Works just fine(case 1)
type someType = 'a' | 'b';
type someExtendedType = someType | 'c';
function someTypeFunction<T extends someType>(args: T): T
return args;
;
let someType: someType, someExtendedType: someExtendedType;
someTypeFunction(someType);
someTypeFunction(someExtendedType); // Gives me an error(case 2)
So I was wondering why such design decisions are made and what are the implications of the same.
changing function someTypeFunction<T extends someType>(args: T): T return args;;
to function someTypeFunction<T extends someExtendedType>(args: T): T return args;;
works but I am not able to understand how this thing is actually working.
EDIT 1
type someType = 'a' | 'b';
type someOtherType = 'c';
type someUnionType = someType | someOtherType;
type someTypeCore<T extends someUnionType> = type: T ;
type someTypeObj<T extends someType> = a: string & someTypeCore<T>;
type someOtherTypeObj<T extends someOtherType> = b: string, c: string & someTypeCore<T>;
function typeAssertion<T extends someUnionType>(args: someTypeCore<T>): args is someTypeObj<T>
return (args as someTypeObj<T>).a !== undefined; // Gives an error
;
function someTypeFunction<T extends someUnionType>(args: someTypeCore<T>): T
if (typeAssertion(args))
// Do something for someTypeObj
else
// Do Something for someOtherTypeObj
;
return args.type;
;
How do we resolve this.
typescript typescript-typings
add a comment |
I am not able to fully grasp the use of extends keyword in case of union types.
Here is a code snippet explaining the confusion that I have.
class SomeClass
someClassProp: string;
;
class SomeExtendedClass extends SomeClass
someExtendedClassProp: string;
;
function someClassFunction<T extends SomeClass>(args: T): T
return args;
;
let someClass: SomeClass, someExtendedClass: SomeExtendedClass;
someClassFunction(someClass);
someClassFunction(someExtendedClass); // Works just fine(case 1)
type someType = 'a' | 'b';
type someExtendedType = someType | 'c';
function someTypeFunction<T extends someType>(args: T): T
return args;
;
let someType: someType, someExtendedType: someExtendedType;
someTypeFunction(someType);
someTypeFunction(someExtendedType); // Gives me an error(case 2)
So I was wondering why such design decisions are made and what are the implications of the same.
changing function someTypeFunction<T extends someType>(args: T): T return args;;
to function someTypeFunction<T extends someExtendedType>(args: T): T return args;;
works but I am not able to understand how this thing is actually working.
EDIT 1
type someType = 'a' | 'b';
type someOtherType = 'c';
type someUnionType = someType | someOtherType;
type someTypeCore<T extends someUnionType> = type: T ;
type someTypeObj<T extends someType> = a: string & someTypeCore<T>;
type someOtherTypeObj<T extends someOtherType> = b: string, c: string & someTypeCore<T>;
function typeAssertion<T extends someUnionType>(args: someTypeCore<T>): args is someTypeObj<T>
return (args as someTypeObj<T>).a !== undefined; // Gives an error
;
function someTypeFunction<T extends someUnionType>(args: someTypeCore<T>): T
if (typeAssertion(args))
// Do something for someTypeObj
else
// Do Something for someOtherTypeObj
;
return args.type;
;
How do we resolve this.
typescript typescript-typings
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
Becausetype someExtendedType = someType | 'c';
does not extendsomeType
.
– zerkms
Mar 26 at 10:43
add a comment |
I am not able to fully grasp the use of extends keyword in case of union types.
Here is a code snippet explaining the confusion that I have.
class SomeClass
someClassProp: string;
;
class SomeExtendedClass extends SomeClass
someExtendedClassProp: string;
;
function someClassFunction<T extends SomeClass>(args: T): T
return args;
;
let someClass: SomeClass, someExtendedClass: SomeExtendedClass;
someClassFunction(someClass);
someClassFunction(someExtendedClass); // Works just fine(case 1)
type someType = 'a' | 'b';
type someExtendedType = someType | 'c';
function someTypeFunction<T extends someType>(args: T): T
return args;
;
let someType: someType, someExtendedType: someExtendedType;
someTypeFunction(someType);
someTypeFunction(someExtendedType); // Gives me an error(case 2)
So I was wondering why such design decisions are made and what are the implications of the same.
changing function someTypeFunction<T extends someType>(args: T): T return args;;
to function someTypeFunction<T extends someExtendedType>(args: T): T return args;;
works but I am not able to understand how this thing is actually working.
EDIT 1
type someType = 'a' | 'b';
type someOtherType = 'c';
type someUnionType = someType | someOtherType;
type someTypeCore<T extends someUnionType> = type: T ;
type someTypeObj<T extends someType> = a: string & someTypeCore<T>;
type someOtherTypeObj<T extends someOtherType> = b: string, c: string & someTypeCore<T>;
function typeAssertion<T extends someUnionType>(args: someTypeCore<T>): args is someTypeObj<T>
return (args as someTypeObj<T>).a !== undefined; // Gives an error
;
function someTypeFunction<T extends someUnionType>(args: someTypeCore<T>): T
if (typeAssertion(args))
// Do something for someTypeObj
else
// Do Something for someOtherTypeObj
;
return args.type;
;
How do we resolve this.
typescript typescript-typings
I am not able to fully grasp the use of extends keyword in case of union types.
Here is a code snippet explaining the confusion that I have.
class SomeClass
someClassProp: string;
;
class SomeExtendedClass extends SomeClass
someExtendedClassProp: string;
;
function someClassFunction<T extends SomeClass>(args: T): T
return args;
;
let someClass: SomeClass, someExtendedClass: SomeExtendedClass;
someClassFunction(someClass);
someClassFunction(someExtendedClass); // Works just fine(case 1)
type someType = 'a' | 'b';
type someExtendedType = someType | 'c';
function someTypeFunction<T extends someType>(args: T): T
return args;
;
let someType: someType, someExtendedType: someExtendedType;
someTypeFunction(someType);
someTypeFunction(someExtendedType); // Gives me an error(case 2)
So I was wondering why such design decisions are made and what are the implications of the same.
changing function someTypeFunction<T extends someType>(args: T): T return args;;
to function someTypeFunction<T extends someExtendedType>(args: T): T return args;;
works but I am not able to understand how this thing is actually working.
EDIT 1
type someType = 'a' | 'b';
type someOtherType = 'c';
type someUnionType = someType | someOtherType;
type someTypeCore<T extends someUnionType> = type: T ;
type someTypeObj<T extends someType> = a: string & someTypeCore<T>;
type someOtherTypeObj<T extends someOtherType> = b: string, c: string & someTypeCore<T>;
function typeAssertion<T extends someUnionType>(args: someTypeCore<T>): args is someTypeObj<T>
return (args as someTypeObj<T>).a !== undefined; // Gives an error
;
function someTypeFunction<T extends someUnionType>(args: someTypeCore<T>): T
if (typeAssertion(args))
// Do something for someTypeObj
else
// Do Something for someOtherTypeObj
;
return args.type;
;
How do we resolve this.
typescript typescript-typings
typescript typescript-typings
edited Mar 26 at 13:26
Amol Gupta
asked Mar 26 at 10:00
Amol GuptaAmol Gupta
1538 bronze badges
1538 bronze badges
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
Becausetype someExtendedType = someType | 'c';
does not extendsomeType
.
– zerkms
Mar 26 at 10:43
add a comment |
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
Becausetype someExtendedType = someType | 'c';
does not extendsomeType
.
– zerkms
Mar 26 at 10:43
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
Because
type someExtendedType = someType | 'c';
does not extend someType
.– zerkms
Mar 26 at 10:43
Because
type someExtendedType = someType | 'c';
does not extend someType
.– zerkms
Mar 26 at 10:43
add a comment |
1 Answer
1
active
oldest
votes
The extends
in the function constains T
to be a subtype of the specified type. What a subtype means may be a bit surprising for unions.
You have to think of what a subtype relationship really means. When we say that SomeExtendedClass
is a subclass of SomeClass
the implication is that any instance of SomeExtendedClass
is also an instance of SomeClass
So the set of all SomeClass
instances contains the set of all SomeExtendedClass
instances.
But if we take this view of subtypes to unions we see that the union with fewer memebers ("a" | "b"
) is actually subtype of the one with more members ("a" | "b" | "c"
) since all instances of "a" | "b"
are instances of "a" | "b" | "c"
.
We intuitively think the subtype is the one that "has more stuff" but when thinking about unions this intuition fails us , we need to think about the subtype relationship in terms of sets.
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances ofSomeClass
in that circle some instances that areSomeClass
are alsoSomeExtendedClass
instances, but not all. The set ofSomeClass
instances is bigger and contains the set of allSomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)
– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. IfsomeTypeObj
has a type parameter that ca be"a" | "b"
why would it be valid to pass in"c"
? Also, there is otypeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.
– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based ontype
that was passed. Please see the edit.
– Amol Gupta
Mar 26 at 13:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55354313%2fwhat-does-extends-actually-mean-in-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The extends
in the function constains T
to be a subtype of the specified type. What a subtype means may be a bit surprising for unions.
You have to think of what a subtype relationship really means. When we say that SomeExtendedClass
is a subclass of SomeClass
the implication is that any instance of SomeExtendedClass
is also an instance of SomeClass
So the set of all SomeClass
instances contains the set of all SomeExtendedClass
instances.
But if we take this view of subtypes to unions we see that the union with fewer memebers ("a" | "b"
) is actually subtype of the one with more members ("a" | "b" | "c"
) since all instances of "a" | "b"
are instances of "a" | "b" | "c"
.
We intuitively think the subtype is the one that "has more stuff" but when thinking about unions this intuition fails us , we need to think about the subtype relationship in terms of sets.
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances ofSomeClass
in that circle some instances that areSomeClass
are alsoSomeExtendedClass
instances, but not all. The set ofSomeClass
instances is bigger and contains the set of allSomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)
– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. IfsomeTypeObj
has a type parameter that ca be"a" | "b"
why would it be valid to pass in"c"
? Also, there is otypeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.
– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based ontype
that was passed. Please see the edit.
– Amol Gupta
Mar 26 at 13:26
add a comment |
The extends
in the function constains T
to be a subtype of the specified type. What a subtype means may be a bit surprising for unions.
You have to think of what a subtype relationship really means. When we say that SomeExtendedClass
is a subclass of SomeClass
the implication is that any instance of SomeExtendedClass
is also an instance of SomeClass
So the set of all SomeClass
instances contains the set of all SomeExtendedClass
instances.
But if we take this view of subtypes to unions we see that the union with fewer memebers ("a" | "b"
) is actually subtype of the one with more members ("a" | "b" | "c"
) since all instances of "a" | "b"
are instances of "a" | "b" | "c"
.
We intuitively think the subtype is the one that "has more stuff" but when thinking about unions this intuition fails us , we need to think about the subtype relationship in terms of sets.
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances ofSomeClass
in that circle some instances that areSomeClass
are alsoSomeExtendedClass
instances, but not all. The set ofSomeClass
instances is bigger and contains the set of allSomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)
– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. IfsomeTypeObj
has a type parameter that ca be"a" | "b"
why would it be valid to pass in"c"
? Also, there is otypeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.
– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based ontype
that was passed. Please see the edit.
– Amol Gupta
Mar 26 at 13:26
add a comment |
The extends
in the function constains T
to be a subtype of the specified type. What a subtype means may be a bit surprising for unions.
You have to think of what a subtype relationship really means. When we say that SomeExtendedClass
is a subclass of SomeClass
the implication is that any instance of SomeExtendedClass
is also an instance of SomeClass
So the set of all SomeClass
instances contains the set of all SomeExtendedClass
instances.
But if we take this view of subtypes to unions we see that the union with fewer memebers ("a" | "b"
) is actually subtype of the one with more members ("a" | "b" | "c"
) since all instances of "a" | "b"
are instances of "a" | "b" | "c"
.
We intuitively think the subtype is the one that "has more stuff" but when thinking about unions this intuition fails us , we need to think about the subtype relationship in terms of sets.
The extends
in the function constains T
to be a subtype of the specified type. What a subtype means may be a bit surprising for unions.
You have to think of what a subtype relationship really means. When we say that SomeExtendedClass
is a subclass of SomeClass
the implication is that any instance of SomeExtendedClass
is also an instance of SomeClass
So the set of all SomeClass
instances contains the set of all SomeExtendedClass
instances.
But if we take this view of subtypes to unions we see that the union with fewer memebers ("a" | "b"
) is actually subtype of the one with more members ("a" | "b" | "c"
) since all instances of "a" | "b"
are instances of "a" | "b" | "c"
.
We intuitively think the subtype is the one that "has more stuff" but when thinking about unions this intuition fails us , we need to think about the subtype relationship in terms of sets.
edited Mar 26 at 11:49
answered Mar 26 at 10:22
Titian Cernicova-DragomirTitian Cernicova-Dragomir
88.8k5 gold badges71 silver badges85 bronze badges
88.8k5 gold badges71 silver badges85 bronze badges
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances ofSomeClass
in that circle some instances that areSomeClass
are alsoSomeExtendedClass
instances, but not all. The set ofSomeClass
instances is bigger and contains the set of allSomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)
– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. IfsomeTypeObj
has a type parameter that ca be"a" | "b"
why would it be valid to pass in"c"
? Also, there is otypeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.
– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based ontype
that was passed. Please see the edit.
– Amol Gupta
Mar 26 at 13:26
add a comment |
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances ofSomeClass
in that circle some instances that areSomeClass
are alsoSomeExtendedClass
instances, but not all. The set ofSomeClass
instances is bigger and contains the set of allSomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)
– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. IfsomeTypeObj
has a type parameter that ca be"a" | "b"
why would it be valid to pass in"c"
? Also, there is otypeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.
– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based ontype
that was passed. Please see the edit.
– Amol Gupta
Mar 26 at 13:26
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
Hmmm I don't get it. The SomeClass circle need to be the other way round. SomeExtendedClass Instance should be the bigger circle encapsulating SomeClass Instance as SomeExtendedClass Instance will have all the properties of SomeClass Instance
– Amol Gupta
Mar 26 at 11:37
1
1
@AmolGupta You are looking at it wrong. The outer circle contains all instances of
SomeClass
in that circle some instances that are SomeClass
are also SomeExtendedClass
instances, but not all. The set of SomeClass
instances is bigger and contains the set of all SomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)– Titian Cernicova-Dragomir
Mar 26 at 11:43
@AmolGupta You are looking at it wrong. The outer circle contains all instances of
SomeClass
in that circle some instances that are SomeClass
are also SomeExtendedClass
instances, but not all. The set of SomeClass
instances is bigger and contains the set of all SomeExtendedClass
instances. It does not matter who has more properties, the diagrams ilustrate sets.en.wikipedia.org/wiki/Set_(mathematics)– Titian Cernicova-Dragomir
Mar 26 at 11:43
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
could you please look at the edit above and how can i resolve it ?
– Amol Gupta
Mar 26 at 12:18
@AmolGupta The errors you are getting seem valid to me. If
someTypeObj
has a type parameter that ca be "a" | "b"
why would it be valid to pass in "c"
? Also, there is o typeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.– Titian Cernicova-Dragomir
Mar 26 at 12:28
@AmolGupta The errors you are getting seem valid to me. If
someTypeObj
has a type parameter that ca be "a" | "b"
why would it be valid to pass in "c"
? Also, there is o typeAsserion
syntax .. only type guards. Not sure how you can specifically fix that code.. it seems inconsistent to me and without a more real world example I am not sure how to fix it.– Titian Cernicova-Dragomir
Mar 26 at 12:28
So the type assertion would be in an if statement and i would like to conditionally branch based on
type
that was passed. Please see the edit.– Amol Gupta
Mar 26 at 13:26
So the type assertion would be in an if statement and i would like to conditionally branch based on
type
that was passed. Please see the edit.– Amol Gupta
Mar 26 at 13:26
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55354313%2fwhat-does-extends-actually-mean-in-typescript%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
When a class extends another class it means it inherits everything that was there plus may add something more. When you use union - you do not extend a type, you just allow some other independent type to pass as well.
– zerkms
Mar 26 at 10:08
so the question is why does case 2 fail ?
– Amol Gupta
Mar 26 at 10:12
Because
type someExtendedType = someType | 'c';
does not extendsomeType
.– zerkms
Mar 26 at 10:43