Why does typescript give type `never` when excluding extended interface
I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never
when it should be a type with property child
interface Child extends Parent {
child: string
}
interface Parent {
parent: string
}
const a: Child = { child: "", parent: "" }
const b = removeParent(a);
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
return obj as Exclude<T, Parent>;
}
// b is now type never...
This does work:
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
return obj as Without<T, "parent">;
}
However I want a generic solution that doesn't require me writing out the types to exclude.
typescript
add a comment |
I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never
when it should be a type with property child
interface Child extends Parent {
child: string
}
interface Parent {
parent: string
}
const a: Child = { child: "", parent: "" }
const b = removeParent(a);
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
return obj as Exclude<T, Parent>;
}
// b is now type never...
This does work:
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
return obj as Without<T, "parent">;
}
However I want a generic solution that doesn't require me writing out the types to exclude.
typescript
add a comment |
I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never
when it should be a type with property child
interface Child extends Parent {
child: string
}
interface Parent {
parent: string
}
const a: Child = { child: "", parent: "" }
const b = removeParent(a);
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
return obj as Exclude<T, Parent>;
}
// b is now type never...
This does work:
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
return obj as Without<T, "parent">;
}
However I want a generic solution that doesn't require me writing out the types to exclude.
typescript
I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never
when it should be a type with property child
interface Child extends Parent {
child: string
}
interface Parent {
parent: string
}
const a: Child = { child: "", parent: "" }
const b = removeParent(a);
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
return obj as Exclude<T, Parent>;
}
// b is now type never...
This does work:
function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
return obj as Without<T, "parent">;
}
However I want a generic solution that doesn't require me writing out the types to exclude.
typescript
typescript
edited Jan 2 at 16:36
Red Riding Hood
asked Jan 2 at 16:05
Red Riding HoodRed Riding Hood
552415
552415
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you do not want Exclude<T, Parent>
Exclude is essentially:
type Exclude<T, U> = T extends U ? never : T
and as a result, your return type will return never
since T extends Parent
in your usage.
What I think you're really after is:
Pick<T, Exclude<keyof T, keyof Parent>>
so that you're saying 'pick all properties of T that do not exist int he parent'
function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
delete obj.one;
delete obj.two;
return obj;
}
1
Exclude
is really intended for removing branches from a union type. For exampleExclude<string|null|void, null|void>
⇒string
, or for excluding elements from akeyof
type like in this answer.
– Jesse Hallett
Jan 2 at 16:52
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%2f54009493%2fwhy-does-typescript-give-type-never-when-excluding-extended-interface%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
I think you do not want Exclude<T, Parent>
Exclude is essentially:
type Exclude<T, U> = T extends U ? never : T
and as a result, your return type will return never
since T extends Parent
in your usage.
What I think you're really after is:
Pick<T, Exclude<keyof T, keyof Parent>>
so that you're saying 'pick all properties of T that do not exist int he parent'
function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
delete obj.one;
delete obj.two;
return obj;
}
1
Exclude
is really intended for removing branches from a union type. For exampleExclude<string|null|void, null|void>
⇒string
, or for excluding elements from akeyof
type like in this answer.
– Jesse Hallett
Jan 2 at 16:52
add a comment |
I think you do not want Exclude<T, Parent>
Exclude is essentially:
type Exclude<T, U> = T extends U ? never : T
and as a result, your return type will return never
since T extends Parent
in your usage.
What I think you're really after is:
Pick<T, Exclude<keyof T, keyof Parent>>
so that you're saying 'pick all properties of T that do not exist int he parent'
function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
delete obj.one;
delete obj.two;
return obj;
}
1
Exclude
is really intended for removing branches from a union type. For exampleExclude<string|null|void, null|void>
⇒string
, or for excluding elements from akeyof
type like in this answer.
– Jesse Hallett
Jan 2 at 16:52
add a comment |
I think you do not want Exclude<T, Parent>
Exclude is essentially:
type Exclude<T, U> = T extends U ? never : T
and as a result, your return type will return never
since T extends Parent
in your usage.
What I think you're really after is:
Pick<T, Exclude<keyof T, keyof Parent>>
so that you're saying 'pick all properties of T that do not exist int he parent'
function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
delete obj.one;
delete obj.two;
return obj;
}
I think you do not want Exclude<T, Parent>
Exclude is essentially:
type Exclude<T, U> = T extends U ? never : T
and as a result, your return type will return never
since T extends Parent
in your usage.
What I think you're really after is:
Pick<T, Exclude<keyof T, keyof Parent>>
so that you're saying 'pick all properties of T that do not exist int he parent'
function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
delete obj.one;
delete obj.two;
return obj;
}
answered Jan 2 at 16:43
EstebanEsteban
1,98011014
1,98011014
1
Exclude
is really intended for removing branches from a union type. For exampleExclude<string|null|void, null|void>
⇒string
, or for excluding elements from akeyof
type like in this answer.
– Jesse Hallett
Jan 2 at 16:52
add a comment |
1
Exclude
is really intended for removing branches from a union type. For exampleExclude<string|null|void, null|void>
⇒string
, or for excluding elements from akeyof
type like in this answer.
– Jesse Hallett
Jan 2 at 16:52
1
1
Exclude
is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>
⇒ string
, or for excluding elements from a keyof
type like in this answer.– Jesse Hallett
Jan 2 at 16:52
Exclude
is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>
⇒ string
, or for excluding elements from a keyof
type like in this answer.– Jesse Hallett
Jan 2 at 16:52
add a comment |
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%2f54009493%2fwhy-does-typescript-give-type-never-when-excluding-extended-interface%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