Welcome to FullStack. We use cookies to enable better features on our website. Cookies help us tailor content to your interests and locations and provide other benefits on the site. For more information, please see our Cookies Policy and Privacy Policy.
Make useContext Data More Discoverable with Typescript
Written by
FullStack Talent
Last updated on:
October 1, 2025
Written by
Last updated on:
October 1, 2025
React’s useContext hook allows developers to manage global application state without the need for a full state-management system, although many such libraries now use useContext under the hood. Data used in deeply-nested components that does not share the same parent component is a great use case for useContext. Storing this data with useContext saves developers from prop-drilling, an anti-pattern that passes large amounts of data down from parent components to deeply-nested components, and all the components in between.
A downside of this pattern is that the type and shape of the data being passed via context is not entirely clear. For example, suppose we have the following contexts: user and company defined at the app root. The location of the company and user data is not relevant. Let’s assume they have been initialized and passed in from a useEffect call.
Now assume a component like TitleContent makes use of user and company to display the company.name and user.fullName. At this point, a developer new to the project would not know what other properties exist on the user or company objects, because nothing in the code conveys this within the file. The names and types of properties available on each object are not available.
In a standard prop-drilling scenario, prop-types could be used to convey the shape and type of the data. However, we are using context in this example, so we are unable to convey this information through the code.
Enter Typescript
Typescript is a strongly typed superset of Javascript that will allow us to convey the types and shapes ofuseContextdata. Check out the Typescript docs to learn more. We can leverage Typescript’s Type Aliases to define the properties available on theuser and company object. We will define the types here so they can be imported and shared across the application.
export type User = {
id: number;
fullName: string;
firstName: string;
lastName: string;
isAdmin: boolean;
logout: () =>void;
};
export type Company = {
name: string;
location: {
street: string;
city: string;
state: string; };
};
The Typescript Partialallows the context to initialize with an empty object. The data will later initialize fully through the context Provider.
Use the following syntax to add the above Type Aliases to the createContext calls.
Next, define the App component in Typescript and give it the appropriate types. Note the introduction of React.FC and AppProps, which expect Company and User types. The types defined above for the Company and User objects are reused and ultimately passed into the provider’s value prop to set the context’s values.
Now when we leverage user or company in another component, our IDE (Visual Studio Code shown) will show auto-completion properties automatically.
Conclusion
By leveraging Typescript we can make data stored with the useContext hook to make the available properties more discoverable. The value of adding types with Typescript is not restricted to the useContext hook. useReducer, useState and other hooks, components and functions can also take advantage of strong typing.
--- At FullStack Labs, we pride ourselves on our ability to push the capabilities of cutting-edge frameworks like React. Interested in learning more about speeding up development time on your next project? contact us.
The useContext hook allows developers to manage global state in a React application without prop-drilling. It’s useful for passing data to deeply nested components that don’t share a common parent component, making state management simpler and more efficient.
What is the problem with useContext data in plain JavaScript?
Without strong typing, the type and shape of data passed through context are not obvious. Developers may not know which properties exist on objects like user or company, and IDEs cannot provide helpful auto-completion or type-checking. This can make working with context less discoverable and error-prone.
How does TypeScript help make useContext data more discoverable?
TypeScript allows developers to define the shape and types of data stored in context. By creating Type Aliases for objects like user and company, and using partial types in createContext, developers can ensure that IDEs provide auto-completion and type-checking for context values throughout the application.
Can TypeScript types be reused across multiple components?
Yes. Once Type Aliases for your context data are defined, they can be imported and shared across your React application. This makes it easy for multiple components to access context data with consistent type safety and discoverable properties.
Are TypeScript benefits limited to useContext?
No. TypeScript can improve discoverability and type safety across many React hooks and components, including useState, useReducer, and custom hooks. Adding types helps prevent errors, improves auto-completion, and makes large codebases easier to maintain.
AI is changing software development.
The Engineer's AI-Enabled Development Handbook is your guide to incorporating AI into development processes for smoother, faster, and smarter development.
Enjoyed the article? Get new content delivered to your inbox.
Subscribe below and stay updated with the latest developer guides and industry insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.