The right way to import "FeedViewPost" in Typescript project? #4922
|
Currently in my Typescript project I am importing import { FeedViewPost } from '@atproto/api/dist/client/types/app/bsky/feed/defs';Is there an alternative approach that doesn't require a deep import path? Maybe something like (open to alternatives): import { FeedDefs } from '@atproto/api';
const FeedViewPost = FeedDefs.FeedViewPostAdditionally the import style upsets Typescript with the following sort of warning:
Note, I explored a bit more, but this doesn't work either: import { AppBskyFeedDefs } from '@atproto/api';
const FeedViewPost = AppBskyFeedDefs.FeedViewPost; |
Replies: 1 comment 1 reply
|
Yes — avoid the deep import. The top-level package already exposes the generated namespaces; the important bit is to use them as types, not as runtime values: import type { AppBskyFeedDefs } from '@atproto/api'
type FeedViewPost = AppBskyFeedDefs.FeedViewPostor inline: function render(post: AppBskyFeedDefs.FeedViewPost) {
// ...
}Why your second attempt failed: const FeedViewPost = AppBskyFeedDefs.FeedViewPost
So the rule of thumb is:
|
|
Hi @ajmas , the deep import (
When you tried import --> Runtime guards are generated the same way, e.g. This is the pattern the official tooling uses, too. E.g.: |
Hi @ajmas ,
the deep import (
@atproto/api/dist/client/types/...) isn't the intended path — that's why you get the "missing declaration" warnings. The generated types are exposed as namespaces on the package root, so import the namespace and access the type as a member:import { AppBskyFeedDefs } from '@atproto/api'type Post = AppBskyFeedDefs.FeedViewPostWhen you tried import
{ AppBskyFeedDefs } from '@atproto/api'earlier, the namespace itself is correct!FeedViewPostjust lives inside it (AppBskyFeedDefs.FeedViewPost); it isn't a separate top-level export! Soimport { FeedViewPost } from '@atproto/api'won't work.--> Runtime guards are generated the same way, e.g.
AppBskyFeedDefs.isPo…