or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-tuple-types.mdbasic-types.mdchange-case-types.mddeep-wrapper-types.mdfunction-types.mdindex.mdkey-types.mdmark-wrapper-types.mdtype-checkers.mdutility-functions.mdutility-types.md

mark-wrapper-types.mddocs/

0

# Mark Wrapper Types

1

2

Transform specific properties of objects by changing their modifiers (optional, readonly, required, writable). These types work on individual property keys rather than entire objects.

3

4

## Capabilities

5

6

### MarkOptional

7

8

Constructs a type by picking all properties from type `Type` where properties `Keys` are set as optional, meaning they aren't required.

9

10

```typescript { .api }

11

type MarkOptional<Type, Keys extends keyof Type> = Prettify<

12

Omit<Type, Keys> & Partial<Pick<Type, Keys>>

13

>;

14

```

15

16

**Usage Example:**

17

18

```typescript

19

import type { MarkOptional } from "ts-essentials";

20

21

type User = {

22

id: number;

23

name: string;

24

email: string;

25

avatar: string;

26

};

27

28

type UserInput = MarkOptional<User, "id" | "avatar">;

29

// Result: { name: string; email: string; id?: number; avatar?: string; }

30

31

const newUser: UserInput = {

32

name: "Alice",

33

email: "alice@example.com"

34

// id and avatar are optional

35

};

36

```

37

38

### MarkReadonly

39

40

Constructs a type by picking all properties from type `Type` where properties `Keys` are set to `readonly`, meaning they cannot be reassigned.

41

42

```typescript { .api }

43

type MarkReadonly<Type, Keys extends keyof Type> = Prettify<

44

Omit<Type, Keys> & Readonly<Pick<Type, Keys>>

45

>;

46

```

47

48

**Usage Example:**

49

50

```typescript

51

import type { MarkReadonly } from "ts-essentials";

52

53

type User = {

54

id: number;

55

name: string;

56

email: string;

57

lastLogin: Date;

58

};

59

60

type UserEntity = MarkReadonly<User, "id" | "lastLogin">;

61

// Result: { name: string; email: string; readonly id: number; readonly lastLogin: Date; }

62

63

const user: UserEntity = {

64

id: 1,

65

name: "Alice",

66

email: "alice@example.com",

67

lastLogin: new Date()

68

};

69

70

user.name = "Bob"; // OK

71

user.email = "bob@example.com"; // OK

72

// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property

73

// user.lastLogin = new Date(); // Error: Cannot assign to 'lastLogin' because it is a read-only property

74

```

75

76

### MarkRequired

77

78

Constructs a type by picking all properties from type `Type` where properties `Keys` are set as required.

79

80

```typescript { .api }

81

type MarkRequired<Type, Keys extends keyof Type> = Prettify<

82

Omit<Type, Keys> & Required<Pick<Type, Keys>>

83

>;

84

```

85

86

**Usage Example:**

87

88

```typescript

89

import type { MarkRequired } from "ts-essentials";

90

91

type UserPreferences = {

92

theme?: string;

93

language?: string;

94

notifications?: boolean;

95

privacy?: string;

96

};

97

98

type RequiredUserPreferences = MarkRequired<UserPreferences, "theme" | "language">;

99

// Result: { notifications?: boolean; privacy?: string; theme: string; language: string; }

100

101

const prefs: RequiredUserPreferences = {

102

theme: "dark", // Required

103

language: "en", // Required

104

notifications: true // Optional

105

// privacy is optional

106

};

107

```

108

109

### MarkWritable

110

111

Constructs a type by picking all properties from type `Type` where properties `Keys` remove `readonly` modifier, meaning they can be reassigned.

112

113

```typescript { .api }

114

type MarkWritable<Type, Keys extends keyof Type> = Prettify<

115

Omit<Type, Keys> & Writable<Pick<Type, Keys>>

116

>;

117

```

118

119

**Usage Example:**

120

121

```typescript

122

import type { MarkWritable } from "ts-essentials";

123

124

type ImmutableUser = {

125

readonly id: number;

126

readonly name: string;

127

readonly email: string;

128

readonly createdAt: Date;

129

};

130

131

type EditableUser = MarkWritable<ImmutableUser, "name" | "email">;

132

// Result: { readonly id: number; readonly createdAt: Date; name: string; email: string; }

133

134

const user: EditableUser = {

135

id: 1,

136

name: "Alice",

137

email: "alice@example.com",

138

createdAt: new Date()

139

};

140

141

user.name = "Bob"; // OK - name is now writable

142

user.email = "bob@example.com"; // OK - email is now writable

143

// user.id = 2; // Error: Cannot assign to 'id' because it is a read-only property

144

// user.createdAt = new Date(); // Error: Cannot assign to 'createdAt' because it is a read-only property

145

```

146

147

## Types

148

149

```typescript { .api }

150

type Writable<Type> = {

151

-readonly [Key in keyof Type]: Type[Key];

152

};

153

154

type Prettify<Type> = Type extends Function

155

? Type

156

: Extract<{

157

[Key in keyof Type]: Type[Key];

158

}, Type>;

159

```