首页 > 其他分享 >[Typescript] Testing type

[Typescript] Testing type

时间:2023-05-07 16:23:27浏览次数:48  
标签:Typescript mount Testing expectType https import type concat

1. TSD: https://github.com/SamVerschueren/tsd

import {expectType} from 'tsd';
import concat from '.';

expectType<string>(concat('foo', 'bar'));
expectType<string>(concat(1, 2));

 

2. Vitest: https://vitest.dev/guide/testing-types.html

import { assertType, expectTypeOf } from 'vitest'
import { mount } from './mount.js'

test('my types work properly', () => {
  expectTypeOf(mount).toBeFunction()
  expectTypeOf(mount).parameter(0).toMatchTypeOf<{ name: string }>()

  // @ts-expect-error name is a string
  assertType(mount({ name: 42 }))
})

 

3. Small utils type helpers:

type Expect<T extends true> = T;
type Equal<X, Y> = Expect<X extends Y ? (Y extends X ? true : false) : false>;

// Example

const identityFunc = <T>(arg: T) => arg;

it("Should return whatever you pass in", () => {
  const test1 = identityFunc("hello");

  type test = Expect<Equal<typeof test1, "hello">>;
});

 

Refer to: https://www.totaltypescript.com/how-to-test-your-types

标签:Typescript,mount,Testing,expectType,https,import,type,concat
From: https://www.cnblogs.com/Answer1215/p/17379474.html

相关文章

  • [Typescript] Builder pattern 07- Reducer
    import{Expect,Equal}from"../types/utils"import{expect,it}from'vitest';typePayloadsToDiscriminatedUnion<TextendsRecord<string,any>>={[KinkeyofT]:{type:K}&T[K];}[keyofT];exportclas......
  • [React Typescript] ComponentProps
    Blog:https://www.totaltypescript.com/react-component-props-type-helper GetanyProptypefromhtmlelement:import{ComponentProps}from"react";typeButtonProps=ComponentProps<"button">; GetpropstypefromaComponen......
  • 与chatGPT讨论c#的calss 与TypeScript中的calss问题
    与chatGPT讨论c#的calss与TypeScript中的calss问题目前来说chatGPT可以说是一场革命,它为我们提供了另一种选项,当你遇到问题的时候,只能去百度但度出来的结果大多不是我们想要的结果,特别是百度当你想要问度娘时......等到的却是一大堆广告,心中不由升起无明之火,不知百度能不能也搞......
  • mac M2 mule esb 3.9 Bad CPU type in executable
    启动直接报错linux由于是amd64linux版本不支持cpu指令集找到合适的theJavaServiceWrapper做转换https://wrapper.tanukisoftware.com/doc/english/download.jsp#stable下载解压warapper.jar移动并改名lib/bootwarapper移动并改名lib/boot/exec改名......
  • rgi main --input_sequence temp/out_pro.fa --output_file result/protein --inpu
    这是一个命令行命令,用于对temp/out_pro.fa文件进行抗菌基因分析。参数的含义如下:rgi:表示运行resistantgeneidentifier(rgi)程序。main:指定使用rgi的主要模式。--input_sequencetemp/out_pro.fa:指定输入序列文件名和路径。--output_fileresult/protein:指......
  • TypeError: 'numpy.float64' object cannot be interpreted as an integer
    报错内容:Traceback(mostrecentcalllast):File"C:\Users\xuan\.conda\envs\pytorch1-6\lib\site-packages\scipy\sparse\_sputils.py",line225,inisintlikeoperator.index(x)TypeError:'numpy.float64'objectcannotbeinterpre......
  • Uncaught TypeError: f.__fbeventsModules[a] is not a function at f.__fbeventsM
    UncaughtTypeError:f.__fbeventsModules[a]isnotafunctionatf.__fbeventsModules.f.getFbeventsModules怎么了这个错误通常是因为代码中使用了Facebook的跟踪代码,但是在加载该代码之前,代码中尝试访问跟踪模块。这个错误有几种可能的原因:Facebook跟踪代码没有正......
  • org.apache.maven.archetypes:maven-archetype-quickstart 这个是干啥的
    org.apache.maven.archetypes:maven-archetype-quickstart是一个Maven项目的骨架(archetype),用于快速创建一个简单的Maven项目。当我们在命令行执行:bashmvnarchetype:generate-DgroupId=com.example-DartifactId=my-project-Darchetype......
  • typescript
    什么是typescripttypescript是微软开发的编程语言,它的后缀名是ts,通过编译可以将ts文件编译成ts文件,它定义了一些新语法使得开发起来可维护性更高也更好用,ts与js的区别如下图所示:在进行angular开发时,开发者不需要引入ts依赖,angular已经导入了ts依赖。快速上手ts编译tschell......
  • Module build failed (from ./node_modules/css-loader/dist/cjs.js): TypeError: thi
    Modulebuildfailed(from./node_modules/css-loader/dist/cjs.js):TypeError:this.getOptionsisnotafunctionModulebuildfailed(from./node_modules/css-loader/dist/cjs.js):TypeError:this.getOptionsisnotafunction 用了各种办法,没有解决问题,直接把node_m......