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

ISSUE: CorpComponent - Video 101. Avoiding assertions in typescript for company name. #4

Open
Dev-Dipesh opened this issue Mar 30, 2024 · 0 comments

Comments

@Dev-Dipesh
Copy link

In the video it was recommended to use ! for type assertion since we know better. But in real-world when multiple people are working on a project, mistakes can happen. Last thing we want is our UI to crash because of it. There is a better way to handle this.

Recommended in the video

const companyName = text.split(" ").find((word) => word.includes("#"))!.substring(1);
const newItem = TFeedbackItem = {
    text: text,
    upvoteCount: 0,
    daysAgo: 0,
    companyName: companyName,
    badgeLetter: companyName.substring(0, 1).toUpperCase(),
}

A better approach is to use ? and use || "" for better error handling. Also, badgeLetter can be written much more simply.

const company = text.split(" ").find(word => word.includes("#"))?.substring(1) || "";
const newFeedback: TFeedbackItem = {
    text,      
    company,
    badgeLetter: company[0].toUpperCase(),
    upvoteCount: 0,
    daysAgo: 0,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant