You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/2025-02-14-typescript-sdk-release.md
+17-23Lines changed: 17 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,37 +148,31 @@ export interface Main {
148
148
}
149
149
```
150
150
151
-
Notice how the `$type` property is defined as optional (`?:`) here. This is due to the fact that lexicons can define schemas that can be referenced in places other than open unions. In those places, there might not be any ambiguity as to how the data should be interpreted. For example, an embed that represents a "Record With Media" has a `record` property that expects an `app.bsky.embed.record` object:
151
+
Notice how the `$type` property is defined as optional (`?:`) here. This is due to the fact that the schema definitions are not always used from open unions. In some cases, a particular schema can be referenced from another schema (using a `"type": "ref"`). In those cases, there will be no ambiguity as to how the data should be interpreted.
152
152
153
-
```typescript
154
-
exportinterfaceMain {
155
-
$type?:'app.bsky.embed.recordWithMedia'
156
-
record:AppBskyEmbedRecord.Main
157
-
media:/* omitted */
158
-
}
159
-
```
160
-
161
-
Since there is no ambiguity as to the type of the data here, making the `$type` property required would cause unnecessary bloat. Making the `$type` property optional allows declaring a "Record With Media" as follows:
153
+
For example, a "Bluesky Like" (`app.bsky.feed.like`) defines the following properties in its schema:
162
154
163
155
```typescript
164
-
const recordWithMedia:$Typed<RecordWithMedia> = {
165
-
// $type is required here because of the $Typed<> utility
// $type is not needed here, as there is no ambiguity
162
+
As can be seen, the `subject` property is defined as a reference to a `com.atproto.repo.strongRef` object. In this case, there is no ambiguity as to how the `subject` of a like should be interpreted, and the `$type` property is not needed.
170
163
171
-
record: {
172
-
/* omitted */
173
-
},
174
-
},
175
-
media: {
176
-
/* omitted */
177
-
},
164
+
```javascript
165
+
{
166
+
"createdAt":"2021-09-01T12:34:56Z",
167
+
"subject": {
168
+
// No `$type` property needed here
169
+
"uri":"at://did:plc:123/app.bsky.feed.post/456",
170
+
"cid":"[...]"
171
+
}
178
172
}
179
173
```
180
174
181
-
Because the `$type` property of objects is required in some contexts while optional in others, we introduced a new utility type to make it required when needed. The `$Typed` utility allows marking an interface’s `$type` property non-optional in contexts where it is required:
175
+
Because the `$type` property of objects is required in some contexts while optional in others, we introduced a new type utility type to make it required when needed. The `$Typed` utility allows marking an interface’s `$type` property non-optional in contexts where it is required:
0 commit comments