Skip to content
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

[TypeScript] Stronger overall typing #36

Open
beefchimi opened this issue Jan 20, 2022 · 0 comments
Open

[TypeScript] Stronger overall typing #36

beefchimi opened this issue Jan 20, 2022 · 0 comments
Labels
✨ Feature New addition to the codebase (feature, utility, component, etc.) 🧑‍💻 Help wanted Extra attention is needed 🧐 Question Further information is requested

Comments

@beefchimi
Copy link
Owner

I have a user case where I want to keep a enum of the supported SocialNetworks in my app.

I want to be able to assert that, when I initialize Socialite with a collection of networks (or add a new network via addNetwork()), Socialite knows exactly which networks exist.

Something like:

import { Socialite, socialiteNetworks } from 'socialitejs';

export const SocialNetwork = {
    Instagram: 'instagram',
    Twitter: 'twitter',
    YouTube: 'youtube'
} as const;

export const socialNetworkKeys = Object.values(SocialNetwork);
export type SupportedSocialNetworks = typeof socialNetworkKeys[number];

const socialNetworks = socialNetworkKeys.map((key) => socialiteNetworks[key]);

const socialParser = new Socialite<SupportedSocialNetworks>(socialiteNetworks);

// Can only be `false`
const facebook = socialParser.getNetwork('facebook');

// Can also only be `false`
const hasFacebook = socialParser.hasNetwork('facebook');

// Can only be `SocialiteNetwork` (since it exists)
const twitter = socialParser.getNetwork('twitter');

// Can also only be `SocialiteNetwork` (since it exists)
const youtube = socialParser.getNetwork(SocialNetwork.YouTube);

// Returns with a type that indicates exactly what networks I have.
const allNetworks = socialParser.getAllNetwork();

Alternatively, if there is a way for Socialite to do this internally - without needing to pass a Generic - that would be incredible!

@beefchimi beefchimi added ✨ Feature New addition to the codebase (feature, utility, component, etc.) 🧐 Question Further information is requested 🧑‍💻 Help wanted Extra attention is needed labels Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature New addition to the codebase (feature, utility, component, etc.) 🧑‍💻 Help wanted Extra attention is needed 🧐 Question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant