-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not possible to narrow records down when wanting to listen to everything #7
Comments
This is difficult unfortunately — it's possible to receive records with an unknown You can somewhat work around it with a type guard like this: type ExtractByType<Record extends { $type: string }, Type extends string> =
Record extends Record
? string extends Record["$type"] ? never : Record & { $type: Type }
: never;
function is<Type extends string, T extends { $type: string }>(record: T, type: Type): record is ExtractByType<T, Type> {
return record.$type === type;
}
if (is(record, "app.bsky.feed.post")) {
record.text;
} I'm not sure I'll add this into the library, so I'll leave this issue up for other users to find. |
Another option would be to pull the the atproto lexicon utils: import {AppBskyFeedPost} from '@atproto/lexicon'
if (AppBskyFeedPost.isRecord(record)) {
record.text; // => type 'string'
} But that only checks the if (AppBskyFeedPost.validateRecord(record).success) {
record.text; // => type 'string'
} The latter is more of a perf hit though, so avoid it in critical paths. If you're reading data from our APIs or firehose, you can trust the more simple and fast |
Only problem is that this library uses Mary's atcute, not |
Oh my mistake! Sorry about that 😅 |
Description
It does not seem possible to narrow down the union of records when wanting to listen to everything:
I would have expected
record
to be of typeAppBskyFeedPost.Record
, but it was stillAppBskyActorProfile.Record | AppBskyFeedGenerator.Record | AppBskyFeedLike.Record | AppBskyFeedPost.Record | AppBskyFeedPostgate.Record | AppBskyFeedRepost.Record | AppBskyFeedThreadgate.Record | AppBskyGraphBlock.Record | AppBskyGraphFollow.Record | AppBskyGraphList.Record | AppBskyGraphListblock.Record | AppBskyGraphListitem.Record | AppBskyGraphStarterpack.Record | AppBskyLabelerService.Record | ChatBskyActorDeclaration.Record
.tsconfig.json:
For now, just doing
"text" in record
suffices, I guess... unless I am being oblivious to something? If so, please let me know!As a side note, I must skip the library check when type checking because it gives compile errors from partysocket. It's not strictly relevant to this package I guess.
Versions
The text was updated successfully, but these errors were encountered: