Skip to content

Commit 25b1847

Browse files
Improved clarity
1 parent b9e677a commit 25b1847

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

blog/2025-02-14-typescript-sdk-release.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,31 @@ export interface Main {
148148
}
149149
```
150150

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.
152152

153-
```typescript
154-
export interface Main {
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:
162154

163155
```typescript
164-
const recordWithMedia: $Typed<RecordWithMedia> = {
165-
// $type is required here because of the $Typed<> utility
166-
$type: 'app.bsky.embed.recordWithMedia',
156+
"properties": {
157+
"createdAt": { "type": "string", "format": "datetime" },
158+
"subject": { "type": "ref", "ref": "com.atproto.repo.strongRef" }
159+
},
160+
```
167161

168-
record: {
169-
// $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.
170163

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+
}
178172
}
179173
```
180174

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:
182176

183177
```typescript
184178
export type $Typed<V> = V & { $type: string }

0 commit comments

Comments
 (0)