[Typescript] Write clean Type 2 - Reduce the usage in generic slot

This the following code example, test have passed for both run time and compile time:

import { expect, it } from 'vitest';
import { Equal, Expect } from '../helpers/type-utils';

function youSayGoodbyeISayHello<
  T extends 'hello' | 'goodbye',
  RES = T extends 'hello' ? 'goodbye' : T extends 'goodbye' ? 'hello' : T
>(greeting: T) {
  return (greeting === 'goodbye' ? 'hello' : 'goodbye') as RES;
}

it('Should return goodbye when hello is passed in', () => {
  const result = youSayGoodbyeISayHello('hello');

  type test = [Expect<Equal<typeof result, 'goodbye'>>];

  expect(result).toEqual('goodbye');
});

it('Should return hello when goodbye is passed in', () => {
  const result = youSayGoodbyeISayHello('goodbye');

  type test = [Expect<Equal<typeof result, 'hello'>>];

  expect(result).toEqual('hello');
});

Something we want to improve in the type level:

We use RES in generic slot, it is just for the return type.

 

We can also use another approach which might be cleaner.

type GreetingType<T extends 'hello' | 'goodbye'> = T extends 'hello'
  ? 'goodbye'
  : T extends 'goodbye'
  ? 'hello'
  : T;

function youSayGoodbyeISayHello<T extends 'hello' | 'goodbye'>(greeting: T) {
  return (greeting === 'goodbye' ? 'hello' : 'goodbye') as GreetingType<T>;
}

In this version, we have only one genric slot, which looks cleaner.

posted @ 2023-04-11 21:09  Zhentiw  阅读(8)  评论(0编辑  收藏  举报