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

发布时间 2023-04-11 21:15:34作者: Zhentiw

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.