首页 > 其他分享 >[Typescript] Force to valid the type by using Valid branded type

[Typescript] Force to valid the type by using Valid branded type

时间:2023-01-04 16:01:08浏览次数:55  
标签:Typescript Force Valid values const PasswordValues type createUserOnApi

Let's we have a form to submit new password. Before we send request to server, we want to force developer to valid the password before sending the request. We can achieve that by using Branded type

declare const brand: unique symbol;
export type Brand<T, TBrand> = T & { [brand]: TBrand };
export type Valid<T> = Brand<T, "Valid">;

 

So for createUserOnApi function only accpet Valid<PasswordValues>, which force developer to call validatePasswordin advance. 

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

const validatePassword = (values: PasswordValues): Valid<PasswordValues> => {
  if (values.password !== values.confirmPassword) {
    throw new Error("Passwords do not match");
  }

  return values as Valid<PasswordValues>;
};

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) => {
    const validatedValues = validatePassword(values);
    createUserOnApi(validatedValues);
  };
});

 

标签:Typescript,Force,Valid,values,const,PasswordValues,type,createUserOnApi
From: https://www.cnblogs.com/Answer1215/p/17025109.html

相关文章

  • 1.4 vp Codeforces Round #838 (Div. 2)
    A-DivideandConquer题意:给出序列a,设b为a中元素总和。你可以选择a中任意元素,将它除以二(向下取整)。问最少需要多少次可以使b为偶数思路:将a划分为奇偶两个集合。a中偶数......
  • Sonatype Nexus Repository Manager
    SonatypeNexus3RepositoryManager发布npm私包-简书(jianshu.com)使用【SonatypeNexusRepositoryManager】搭建内部NPM源-CoderMonkey-博客园(cnblogs.co......
  • Codeforces Hello 2023 CF 1779 A~F 题解
    点我看题A.HallofFame先把不用操作就合法的情况判掉。然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置。所以我们就是要找到一个位置i,......
  • Codeforces Hello 2023 CF 1779 A~F 题解
    点我看题A.HallofFame先把不用操作就合法的情况判掉。然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置。所以我们就是要找到一个位置i,......
  • [python] TypeError: expected str, bytes or os.PathLike object, not NoneType
    参考链接:https://blog.csdn.net/weixin_42345113/article/details/104514545出现这个问题多半是没有指定路径,上述问题翻译过来是,期望一个字符串或者字节路径,而不是默认值,......
  • RTP有效负载(载荷)类型 (RTP Payload Type)
    下图为RTP部格式:*****************************************************1)    V:RTP协议的版本号,占2位,当前协议版本号为22)    P:填充标志,占1位,如果P=1......
  • ExtJS-所有组件的简写(Xtype值)
    ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.html转载请注明出处:https://www.cnblogs.com/cqpanda/p/16587500.html更新记录2023年1月3日从笔记迁移到......
  • [Typescript] Get full type safe for discriminatedUnion type with 'type' & 'subty
    //printtypePrintStart={type:"print";subtype:"start";attributes:{controlsId:string;tabId:number;};}typePrint......
  • CodeForces 991C Candies(二分答案)
    CodeForces991CCandies(二分答案)http://codeforces.com/problemset/problem/991/C    题意:给出一个数n,表示有n块蛋糕,有两个人a,b。a每次可以取k块蛋糕(如果剩下......
  • 1.3 vp Educational Codeforces Round 139 (Rated for Div. 2)
    A-ExtremelyRound题意:给出n,找出从1到n中,只出现过一次非0数字的数思路:一开始以为是暴力,wa了一发老老实实找规律。就是找最高位,最高位是几,就有几个,再加上,每多一位要加9......