0

I've a BaseType and a BabyType.

export type BaseType = {
    id: string
}
export type BabyType = BaseType & {
    name: string 
};

I have generic layer where <T extends BaseType>. Actually item is a 'BabyType' object.

updateItem(item: T): Promise<T> {
        console.log(data) // {id: '6495f70a317b446b083f', name: 'Test'}

In this layer I want to remove 'id' (dynamically) from the data but I want to keep all key what defined in BabyType.

So the excepted result: console.log(data) // {name: 'Test'}

I've tried

type OmitType = Omit<T, keyof BaseType>;
console.log(data as OmitType)

but it does not work. id still logged in console.

Is there any solution to remove all key/value what are come from BaseType.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
tamke001
  • 15
  • 6
  • Typescript only affects types, not the actual behavior. If you want to delete some property you can do like [this](https://www.typescriptlang.org/play?ts=5.0.4#code/KYDwDg9gTgLgBDAnmYcBCBDAzsAKs1AXjgG8BYAKDmrgEsATALjixiloDsBzAbkoF8+FUJFgIC6DACNE+FHGKYcc1ADJSlGnA4YAtsGat23IYMqUAZgFcOAYxi0IHOFbD0MMYAElPugBS0vsyYMioAlORUNLZOrKQMADRwAHSpUMCs-Ap0vkKaNOms+dQA9CVwAHoA-AKUQA) – wonderflame Jun 24 '23 at 10:15
  • Thank you. Would be nice if I can dinamically set 'id' to there, because I have more key in BaseType. – tamke001 Jun 24 '23 at 10:21
  • okay, let me make more dynamic approach – wonderflame Jun 24 '23 at 10:22
  • Does [this approach](https://www.typescriptlang.org/play?target=6&ts=5.0.4#code/C4TwDgpgBAQghgZwgFXNAvFA3gWAFBSFQCWAJgFxQLABOxAdgOYDc+Avq3qJLHAEYhUPTPCRDoAMmz4iUenAC2EStTpNOs+UoBMK2gxYyiWiAGY9awwWOKIAFgsHOHfPgDGAe3rUoHhcWAoTAAeZCgIAA9gCHpSBF8+ACsIN2AAGigAaXComLioAAoAawgQDwAzKGQASgBtAF0APgKPJMpkDJKQBEpM6qDG6WtCGghgAFcaeigAeSSU4AA6cpo-AFF6fQgEAqNZQjnk1MWYrZ3WxOrl4gAbaJoCgtqijIA3ev70QYBCLoRFhhuG7jUjbYpQRBQLoVKrVappPZQfqQmb+YChDKZWr0cYKPgQGhNZycdxeHxwSjwATiIJDWRkSgAcgAjIyEcM5LYmQg2YiTLooIzSLyOSZzILTCLNLYHIKAKwilx4UneQKjBDjO60vwBApwDK1RlkNmC-kmxli80mOyMj6cTzeDw3CCLG4eRgFdWa4DVThAA) work for you? – wonderflame Jun 24 '23 at 10:27
  • Sorry but I would like to not use any 'key' in this layer. Just the types. – tamke001 Jun 24 '23 at 10:29
  • I can't define keys in this layer. It would be duplication because it's defined at Type definition – tamke001 Jun 24 '23 at 10:29
  • As I mentioned previously, `Omit` won't affect any run time behavior. You will still get the whole object, thus you need actual js code. – wonderflame Jun 24 '23 at 10:30
  • Thanks. If you answer under the post I'll accept that – tamke001 Jun 24 '23 at 14:16

2 Answers2

1

Typescript doesn't affect runtime behavior. Your as assertion only changes how Typescript infers the type but doesn't modify the JavaScript object. This is exactly a showcase of why you should avoid assertions since they can cause misleading types.

The easiest approach is just destructing the item and divide it into id and the rest:

export type BaseType = {
    id: string;
};
export type BabyType = BaseType & {
    name: string;
};

function updateItem(item: BabyType){
    const {id, ...rest} = item;

    rest
    // ^? const rest = {name: string
}
wonderflame
  • 6,759
  • 1
  • 1
  • 17
0

Looks like it's not possible. (without lib)

Get keys of a Typescript interface as array of strings

Thank you @wonderflame for the answers. Finally I used your code.

const {id, ...rest} = item;
tamke001
  • 15
  • 6