Skip to content

Latest commit

 

History

History

mark-required

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

MarkRequired<Type, Keys> constructs a type by picking all properties from type Type where properties Keys are set as required

declare class User {
  id: number;
  posts?: Post[];
  photos?: Photo[];
}

type UserWithPosts = MarkRequired<User, "posts">;
//   ^? { id: number; posts: Post[]; photos?: Photo[] }

A real-life example: when selecting an object with its related entities from ORM

declare const dataSource: DataSource;

const userRepository = dataSource.getRepository(User);

async function getUserWithPosts(id: number): Promise<UserWithPosts> {
  return userRepository.findOneOrFail({ where: { id }, relations: ["posts"] }) as Promise<UserWithPosts>;
}

TS Playground – https://tsplay.dev/Wvqy4m