[Typescript] Combine Branded type with type predicates

import { it } from 'vitest';
import { Brand } from '../helpers/Brand';

type Valid<T> = Brand<T, 'Valid'>;

interface PasswordValues {
  password: string;
  confirmPassword: string;
}

/**
 * 💡 You'll need to change this function...
 */
const isValidPassword = (
  values: PasswordValues
): values is Valid<PasswordValues> => {
  if (values.password !== values.confirmPassword) {
    return false;
  }
  return true;
};

const createUserOnApi = (values: Valid<PasswordValues>) => {
  // Imagine this function creates the user on the API
};

it('Should fail if you do not validate the values before calling createUserOnApi', () => {
  const onSubmitHandler = (values: PasswordValues) => {
    // @ts-expect-error
    createUserOnApi(values);
  };
});

it('Should succeed if you DO validate the values before calling createUserOnApi', () => {
  const onSubmitHandler = (values: PasswordValues) => {
    if (isValidPassword(values)) {
      createUserOnApi(values);
    }
  };
});

 

posted @ 2023-02-10 20:44  Zhentiw  阅读(12)  评论(0编辑  收藏  举报