Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Add isFullUser helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jul 28, 2019
1 parent 497cd88 commit 2a8f8fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.ts
Expand Up @@ -17,3 +17,9 @@ export { UserDescriptionEntity } from './types/user_description_entity';
export { UserEntities } from './types/user_entities';
export { UserMentionEntity } from './types/user_mention_entity';
export { UserUrlEntity } from './types/user_url_entity';

import { FullUser, User } from './types/user';

export function isFullUser(user: User): user is FullUser {
return 'screen_name' in user;
}
23 changes: 20 additions & 3 deletions readme.md
Expand Up @@ -15,7 +15,7 @@ _Note that the presence of field is not an indication of it being current and ac
Install
----

```
```bash
npm install --save-dev twitter-d
```

Expand All @@ -24,7 +24,7 @@ Usage

The main two interfaces provided are [`Status`](https://github.com/abraham/twitter-d/blob/master/types/status.d.ts) and [`User`](https://github.com/abraham/twitter-d/blob/master/types/user.d.ts) although there are a number of related interfaces like [`MediaEntity`](https://github.com/abraham/twitter-d/blob/master/types/media_entity.d.ts) and [`AdditionalMediaInfo`](https://github.com/abraham/twitter-d/blob/master/types/additional_media_info.d.ts).

```
```typescript
import { Status as Tweet, User } from 'twitter-d';

function getTweet(): Tweet { /* ... */ }
Expand All @@ -33,10 +33,27 @@ function getUser(): User { /* ... */ }

Or if you are using [TypeScript 2.9+](https://blogs.msdn.microsoft.com/typescript/2018/05/31/announcing-typescript-2-9/#import-types) you can use `import()`.

```
```typescript
function getStatus(): import('twitter-d').Status { /* ... */ }
```

User
----

In some contexts, using `trim_user=true` on [GET statuses/mentions_timeline](https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-mentions_timeline), you might get a user object that only includes `id` and `id_str`. To test if a `User` value is a `FullUser` you can use the `isFullUser` helper function.

```typescript
import { isFullUser, User } from 'twitter-d';

function logUser(user: User) {
if (isFullUser(user)) {
console.log(user.name);
} else {
throw new Error('User is not type FullUser');
}
}
```

Contributing
----

Expand Down

0 comments on commit 2a8f8fa

Please sign in to comment.