How to exclude a key from an interface in TypeScript
In TypeScript, you can combine two interface types like this
interface Foo {
var1: string
}
interface Bar {
var2: string
}
type Combined = Foo & Bar
Instead of combining keys, I want to exclude keys from one interface to another. Is there anyway you can do it in TypeScript?
The reason is, I have an HOC, which manages a property value for other wrapped component like this
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
With that I can write
const ValuedComponent = valueHOC(MyComponent)
then
<ValuedComponent />
but the problem is, the returned component type is also using the props type from given component, so TypeScript will complain and ask me to provide the value
prop. As a result, I will have to write something like
<ValuedComponent value="foo" />
Which the value will not be used anyway. What I want here is to return an interface without specific keys, I want to have something like this
React.ComponentClass<P - {value: string}>
Then the value
will not be needed in the returned component. Is it possible in TypeScript for now?
typescript
add a comment |
In TypeScript, you can combine two interface types like this
interface Foo {
var1: string
}
interface Bar {
var2: string
}
type Combined = Foo & Bar
Instead of combining keys, I want to exclude keys from one interface to another. Is there anyway you can do it in TypeScript?
The reason is, I have an HOC, which manages a property value for other wrapped component like this
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
With that I can write
const ValuedComponent = valueHOC(MyComponent)
then
<ValuedComponent />
but the problem is, the returned component type is also using the props type from given component, so TypeScript will complain and ask me to provide the value
prop. As a result, I will have to write something like
<ValuedComponent value="foo" />
Which the value will not be used anyway. What I want here is to return an interface without specific keys, I want to have something like this
React.ComponentClass<P - {value: string}>
Then the value
will not be needed in the returned component. Is it possible in TypeScript for now?
typescript
1
You may check outPick<T, K keyof T>
. But you can't pick things out dynamically.
– unional
Jul 9 '17 at 6:26
add a comment |
In TypeScript, you can combine two interface types like this
interface Foo {
var1: string
}
interface Bar {
var2: string
}
type Combined = Foo & Bar
Instead of combining keys, I want to exclude keys from one interface to another. Is there anyway you can do it in TypeScript?
The reason is, I have an HOC, which manages a property value for other wrapped component like this
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
With that I can write
const ValuedComponent = valueHOC(MyComponent)
then
<ValuedComponent />
but the problem is, the returned component type is also using the props type from given component, so TypeScript will complain and ask me to provide the value
prop. As a result, I will have to write something like
<ValuedComponent value="foo" />
Which the value will not be used anyway. What I want here is to return an interface without specific keys, I want to have something like this
React.ComponentClass<P - {value: string}>
Then the value
will not be needed in the returned component. Is it possible in TypeScript for now?
typescript
In TypeScript, you can combine two interface types like this
interface Foo {
var1: string
}
interface Bar {
var2: string
}
type Combined = Foo & Bar
Instead of combining keys, I want to exclude keys from one interface to another. Is there anyway you can do it in TypeScript?
The reason is, I have an HOC, which manages a property value for other wrapped component like this
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<P> {
return class WrappedComponent extends React.Component<P, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
With that I can write
const ValuedComponent = valueHOC(MyComponent)
then
<ValuedComponent />
but the problem is, the returned component type is also using the props type from given component, so TypeScript will complain and ask me to provide the value
prop. As a result, I will have to write something like
<ValuedComponent value="foo" />
Which the value will not be used anyway. What I want here is to return an interface without specific keys, I want to have something like this
React.ComponentClass<P - {value: string}>
Then the value
will not be needed in the returned component. Is it possible in TypeScript for now?
typescript
typescript
asked Jul 8 '17 at 7:13
Fang-Pen LinFang-Pen Lin
5,50484882
5,50484882
1
You may check outPick<T, K keyof T>
. But you can't pick things out dynamically.
– unional
Jul 9 '17 at 6:26
add a comment |
1
You may check outPick<T, K keyof T>
. But you can't pick things out dynamically.
– unional
Jul 9 '17 at 6:26
1
1
You may check out
Pick<T, K keyof T>
. But you can't pick things out dynamically.– unional
Jul 9 '17 at 6:26
You may check out
Pick<T, K keyof T>
. But you can't pick things out dynamically.– unional
Jul 9 '17 at 6:26
add a comment |
3 Answers
3
active
oldest
votes
In TypeScript 2.8 you can now do the following:
interface Foo {
attribute1: string;
optional2?: string;
excludePlease: string;
}
// Define Omit. Can be defined in a utilities package
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
// Use Omit to exclude one or more fields (use "excludePlease"|"field2"|"field3" etc to exclude multiple)
type Bar = Omit<Foo, "excludePlease">
const b: Bar = {
attribute1: ''
};
So in relation to your question the following might be what you want:
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<Omit<P, "value">> {
return class WrappedComponent extends React.Component<Omit<P, "value">, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplifiedOmit
code in the simplify example. Is that better now?
– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
add a comment |
You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value
property set as optional will return an error.
To avoid this issue, modify the typings of the target component so value
property is optional.
e.g.
// ...
class MyComponent extends React.Component<{value?: string}, State> {
// ...
}
and then component produced when using High Order Function
const valuedComponent = valueHOC(MyComponent);
won't ask for value
prop.
add a comment |
There is utility-types library that has Substract
mapped type:
import { Subtract } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type RequiredProps = Subtract<Props, DefaultProps>;
// Expect: { name: string; visible: boolean; }
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%2f44983560%2fhow-to-exclude-a-key-from-an-interface-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In TypeScript 2.8 you can now do the following:
interface Foo {
attribute1: string;
optional2?: string;
excludePlease: string;
}
// Define Omit. Can be defined in a utilities package
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
// Use Omit to exclude one or more fields (use "excludePlease"|"field2"|"field3" etc to exclude multiple)
type Bar = Omit<Foo, "excludePlease">
const b: Bar = {
attribute1: ''
};
So in relation to your question the following might be what you want:
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<Omit<P, "value">> {
return class WrappedComponent extends React.Component<Omit<P, "value">, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplifiedOmit
code in the simplify example. Is that better now?
– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
add a comment |
In TypeScript 2.8 you can now do the following:
interface Foo {
attribute1: string;
optional2?: string;
excludePlease: string;
}
// Define Omit. Can be defined in a utilities package
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
// Use Omit to exclude one or more fields (use "excludePlease"|"field2"|"field3" etc to exclude multiple)
type Bar = Omit<Foo, "excludePlease">
const b: Bar = {
attribute1: ''
};
So in relation to your question the following might be what you want:
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<Omit<P, "value">> {
return class WrappedComponent extends React.Component<Omit<P, "value">, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplifiedOmit
code in the simplify example. Is that better now?
– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
add a comment |
In TypeScript 2.8 you can now do the following:
interface Foo {
attribute1: string;
optional2?: string;
excludePlease: string;
}
// Define Omit. Can be defined in a utilities package
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
// Use Omit to exclude one or more fields (use "excludePlease"|"field2"|"field3" etc to exclude multiple)
type Bar = Omit<Foo, "excludePlease">
const b: Bar = {
attribute1: ''
};
So in relation to your question the following might be what you want:
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<Omit<P, "value">> {
return class WrappedComponent extends React.Component<Omit<P, "value">, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
In TypeScript 2.8 you can now do the following:
interface Foo {
attribute1: string;
optional2?: string;
excludePlease: string;
}
// Define Omit. Can be defined in a utilities package
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
// Use Omit to exclude one or more fields (use "excludePlease"|"field2"|"field3" etc to exclude multiple)
type Bar = Omit<Foo, "excludePlease">
const b: Bar = {
attribute1: ''
};
So in relation to your question the following might be what you want:
export default function valueHOC<P> (
Comp: React.ComponentClass<P> | React.StatelessComponent<P>
): React.ComponentClass<Omit<P, "value">> {
return class WrappedComponent extends React.Component<Omit<P, "value">, State> {
render () {
return (
<Comp
{...this.props}
value={this.state.value}
/>
)
}
}
edited Sep 27 '18 at 13:35
answered Jun 4 '18 at 21:37
AJPAJP
9,153155589
9,153155589
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplifiedOmit
code in the simplify example. Is that better now?
– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
add a comment |
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplifiedOmit
code in the simplify example. Is that better now?
– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
1
1
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
Whilst this works, it will make optional parameters required. You can do this to prevent that: stackoverflow.com/a/48216010/616589
– Simon
Aug 14 '18 at 17:07
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
I don't dispute the accuracy of the answer, but that's an absurdly round the houses way of achieving a simple end - it will take anyone reading it some non-trivial amount of time to work out just what the hell is going in. Is there any logic at all as to why the typescript team went this way?
– havlock
Sep 26 '18 at 17:49
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplified
Omit
code in the simplify example. Is that better now?– AJP
Sep 27 '18 at 13:37
@havlock sorry I recently edited it to simplify and improve the answer but I forget to also use the simplified
Omit
code in the simplify example. Is that better now?– AJP
Sep 27 '18 at 13:37
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@AJP I was moaning about typescript, not your answer, but thanks - yes, that is slightly more straightforward. Still absolutely bonkers that they make us jump through such ridiculous syntax to achieve something simple - I fear for the language if they really think this sort of thing is OK.
– havlock
Sep 28 '18 at 6:56
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
@havlock :) Would be interesting to see examples of other typed languages where this is dealt with better :)
– AJP
Sep 28 '18 at 16:41
add a comment |
You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value
property set as optional will return an error.
To avoid this issue, modify the typings of the target component so value
property is optional.
e.g.
// ...
class MyComponent extends React.Component<{value?: string}, State> {
// ...
}
and then component produced when using High Order Function
const valuedComponent = valueHOC(MyComponent);
won't ask for value
prop.
add a comment |
You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value
property set as optional will return an error.
To avoid this issue, modify the typings of the target component so value
property is optional.
e.g.
// ...
class MyComponent extends React.Component<{value?: string}, State> {
// ...
}
and then component produced when using High Order Function
const valuedComponent = valueHOC(MyComponent);
won't ask for value
prop.
add a comment |
You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value
property set as optional will return an error.
To avoid this issue, modify the typings of the target component so value
property is optional.
e.g.
// ...
class MyComponent extends React.Component<{value?: string}, State> {
// ...
}
and then component produced when using High Order Function
const valuedComponent = valueHOC(MyComponent);
won't ask for value
prop.
You can't remove properties from already existing interfaces. Even trying to extend existing interface with the interface having value
property set as optional will return an error.
To avoid this issue, modify the typings of the target component so value
property is optional.
e.g.
// ...
class MyComponent extends React.Component<{value?: string}, State> {
// ...
}
and then component produced when using High Order Function
const valuedComponent = valueHOC(MyComponent);
won't ask for value
prop.
answered Jul 8 '17 at 11:45
Michal PietraszkoMichal Pietraszko
2,39321120
2,39321120
add a comment |
add a comment |
There is utility-types library that has Substract
mapped type:
import { Subtract } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type RequiredProps = Subtract<Props, DefaultProps>;
// Expect: { name: string; visible: boolean; }
add a comment |
There is utility-types library that has Substract
mapped type:
import { Subtract } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type RequiredProps = Subtract<Props, DefaultProps>;
// Expect: { name: string; visible: boolean; }
add a comment |
There is utility-types library that has Substract
mapped type:
import { Subtract } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type RequiredProps = Subtract<Props, DefaultProps>;
// Expect: { name: string; visible: boolean; }
There is utility-types library that has Substract
mapped type:
import { Subtract } from 'utility-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };
type RequiredProps = Subtract<Props, DefaultProps>;
// Expect: { name: string; visible: boolean; }
edited Dec 29 '18 at 8:31
answered Dec 29 '18 at 8:09
ChintsuChintsu
316510
316510
add a comment |
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%2f44983560%2fhow-to-exclude-a-key-from-an-interface-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
1
You may check out
Pick<T, K keyof T>
. But you can't pick things out dynamically.– unional
Jul 9 '17 at 6:26