xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript type-only import All In One

TypeScript type-only import All In One

import type 是 TypeScript 的 type-only import 写法, 表示“只导入类型 UUID,不导入运行时值”。

import type { UUID } from 'crypto';

image

demos

UUID

import type { UUID } from 'crypto';

// node_modules/@types/node/crypto.d.ts
type UUID = `${string}-${string}-${string}-${string}-${string}`;

A Universally Unique IDentifier (UUID) URN Namespace

https://www.rfc-editor.org/rfc/rfc4122.txt

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Nest.js

PartialType

# validation
$ npm install class-validator class-transformer

# ?
$ npm i @nestjs/mapped-types
$ npm i @nestjs/swagger

  • PartialType - returns a type (class) with all the properties of the input type set to optional (requirement: at least 1 validation decorator applied to each property)
  • PickType - constructs a new type (class) by picking a set of properties from an input type
  • OmitType - constructs a type by picking all properties from an input type and then removing a particular set of keys
  • IntersectionType - combines two types into one new type (class)

https://www.npmjs.com/package/@nestjs/mapped-types

https://www.npmjs.com/package/@nestjs/swagger

refs

Introducing Mapped Types for NestJS.

Mapped Types are supported by both REST (@nestjs/swagger) and GraphQL (@nestjs/graphql) applications.

import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, MinLength } from 'class-validator';
export class CreateUserDto {
  @IsEmail()
  @ApiProperty()
  email: string;
  @MinLength(10)
  @ApiProperty()
  password: string;
}
import { CreateUserDto } from './create.profile.dto';
// RESTful API ✅ swagger
import { PartialType } from '@nestjs/swagger';

export class UpdateUserDto extends PartialType(CreateUserDto) {
  // 复用
}

import { InputType, Field } from '@nestjs/graphql';
@InputType()
class CreateUserInput {
  @Field()
  email: string;
  @Field()
  password: string;
}


// GraphQL API ✅ graphql
import { InputType } from '@nestjs/graphql';
import { PartialType } from '@nestjs/graphql';
@InputType()
export class UpdateUserInput extends PartialType(CreateUserInput) {}

https://trilon.io/blog/introducing-mapped-types-for-nestjs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2026-09-20 03:17  xgqfrms  阅读(3)  评论(1)    收藏  举报