Is it somehow possible to create a subtracted type in Typescript? I'm thinking of a user-case when a React component would expose just subset of if props to components user. React-redux connect example:
import {Component, ComponentType} from 'react';
export function connect<S, A>(state: () => S, actions: A){
return function createConnected<P>(component: ComponentType<P>){
return class Connect extends Component<P-S-A>{ // <--
// ...
}
}
}
After reading: Exclude property from type
Seems that I got this working...
import {Component, ComponentType} from 'react';
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
function connect<S, A>(state: () => S, actions: A) {
return function createConnect<P extends S & A>(C: React.ComponentType<P>): ComponentType<Omit<P, keyof S | keyof A>> {
return class Connect extends Component<Omit<P, keyof S | keyof A>> {
// ...
};
};
}
.... But I don't understand how.
Update 2:
After playing with this some more, I discovered a cleaner way (in my opinion), to describe a subtracted type:
// LeftOuterJoin
type Subtract<T, V> = Pick<T, Exclude<keyof T, keyof V>>;
function connect<S, A>(state: () => S, actions: A) {
return function createConnect<P>(C: ComponentType<P>) {
return class Connect extends Component<Subtract<P, S & A>> {
// ...
};
};
}
`
– CRice Aug 06 '18 at 20:57