TypeScript definitions for React, the popular JavaScript library for building user interfaces
80
Build a higher-order component that adds authentication and role-based access control to React components.
withAuth and user prop { isAuthenticated: false }, it displays "Please log in to access this content" @testwithAuth requiring role "admin", user prop { isAuthenticated: true, role: "user" }, it displays "Access denied: insufficient permissions" @testwithAuth requiring role "admin", user prop { isAuthenticated: true, role: "admin" }, it renders the wrapped component @testwithAuth (no role required), user prop { isAuthenticated: true }, it renders the wrapped component with all original props @test@generates
export interface AuthUser {
isAuthenticated: boolean;
role?: string;
}
export interface WithAuthConfig {
requiredRole?: string;
}
export interface WithAuthProps {
user: AuthUser;
}
export function withAuth<P extends object>(
Component: React.ComponentType<P>,
config?: WithAuthConfig
): React.ComponentType<P & WithAuthProps>;Provides TypeScript type definitions for React including ComponentType, higher-order component patterns, and proper prop typing.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-types--reactdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10