Create a Typescript Union Type From a Javascript Object
lessonFree
Browse
Find courses, lessons, articles, talks, and podcasts.
389 results for "typescript"
Showing 169–192 · Page 8 of 17
lessonFree
lesson
lesson
lesson
lesson
course
lessonDuration 1m 44s
Say you have an array that has at least one item repeated. How would you find the repeated item. This is a question commonly presented to beginner developers. Here we discuss the elegant solution to this problem.
lessonDuration 1m 56sFree
Thank you for making it through this course. Please leave your feedback and suggestions! ---- Check out the [React Testing Library Docs](https://testing-library.com/docs/react-testing-library/intro/) and [writing tests section of the redux docs](https://redux.js.org/usage/writing-tests) for more information.
course
In this course, we will be building a React Native news app that is read-only when offline. The videos are recorded on a Mac with an iPhone 13 Simulator using plain React Native, but you can do this course using Android, Windows and even the Expo managed workflow. See the lesson description for platform-specific instructions. The solutions repo has been verified to work on a Pixel 3a Emulator on Mac. The completed app for this course is available [here](https://github.com/kadikraman/news-flash) and each lesson will include a link to the commit that added the code so you can check your work. After we build our news app, we're going to enhance it to make it read-only when offline using urql'g GraphCache. This course will require you to run the api locally on your device. The api repo is available [here](https://github.com/kadikraman/news-flash-api). **Resources:** - urql: https://formidable.com/open-source/urql/docs/ - Insomnia: https://insomnia.rest/ - Async Storage: https://github.com/react-native-async-storage/async-storage - React Native Netinfo: https://github.com/react-native-netinfo/react-native-netinfo
lessonDuration 3m 53s
We are going to start refactoring our `CountDisplay` component. It is a small stateless component but it has a few props that can benefit from type safety. There are three ways to type a component, inline, alias, and as a function expression. The inline typing adds a bit of noise to our code and can make it difficult to parse right out of the gate. To fix this, we use a type alias that reads a little bit nicer. To add in a function expression, which we get from the React Types that we downloaded, we can declare this variable to have a type of `React.FunctionComponent` which takes as a type argument, our props.
lessonDuration 6m 6s
Previously, I added some type information for the utility function we are going to look at. By clicking on the function, a lightbulb appears. We can click that and select `Annotate everything with types from JSDoc`. Thanks to our plugin, it will type our function for us. It's not always perfect so double check to make sure it's exactly as you would like. Along with the utility function, we are going to look at how to type a `useMemo` hook, a `useCallback` hook, a `useRef` hook, a `useState` hook, and a custom hook.
lessonDuration 4m 39s
We get the state of our board from a `reduce` function. It currently has no error but also has no type safety since everything is inferred to be of type `any`. `useReducer` is generic and accepts type arguments, describing the state as well as the events it can expect to receive. The simplest way to add types to a `useReducer` is to add them directly to the `reducer` function itself. We will do this by using an alias for our types.
lessonDuration 6m 42s
Our `Radio`component file contains several compound components to create a related group of radio inputs. The `RadioGroupContext` is receiving a type of `any` currently. This is a great example to peer into how Strict mode works. To see the difference between the strict mode's true and false setting, we will set `strict` to `true`. This will change our `RadioGroupContext` to be of type `null` which will raise some issues later down the line where trying to destructure an object that might be null. This also gives us the opportunity to better our application with error handling. I will write out a couple of custom hooks to help out with that.
lessonDuration 2m 7sFree
Best practices for building & testing redux applications have changed dramatically over time. This course aims to be a comprehensive yet understandable resource for those seeking to improve the testing of their redux applications. This course is broken up into three parts. The intro is look at how to add jest to an existing react redux application that was built on top of [vite](https://vitejs.dev/). We cover basic functionality like [watch mode](https://jestjs.io/docs/cli#--watch) and generating and using [code coverage](https://jestjs.io/docs/cli#--coverageboolean) to inform our testing strategy. The first section covers the specifics of unit testing the redux code in our application. We'll go over how to test reducers, selectors and thunks. As part of this we'll touch on mocking APIs and using [Redux Mock Store](https://github.com/reduxjs/redux-mock-store) as well as running actions through our applications' actual reducers to broaden our test coverage. Finally we'll look at how we can test our redux connected react components using the various utilities that come with the [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/). We'll cover simulating various kinds of user input with the [user-event module](https://testing-library.com/docs/ecosystem-user-event), populating our redux store with test data, using [jest spy](https://jestjs.io/docs/jest-object#jestspyonobject-methodname), how to wait for asynchronous tasks to complete, and several approaches for finding & asserting that visual elements exist on the page. This course is built on top of content that was created for the [Modern Redux with RTK and TypeScript](https://app.egghead.io/playlists/modern-redux-with-redux-toolkit-rtk-and-typescript-64f243c8) course and I highly recommend you start there if you haven't completed it already. I'm confident you will level up your redux testing abilities if watch these lessons and practice coding each of them as you go.
lessonDuration 1m 23sFree
I have a few courses out now about using Redux Hooks, including [Redux with React Hooks](https://egghead.io/courses/redux-with-react-hooks-8a37) and [Modernizing a Legacy Redux Application with Redux Hooks](https://egghead.io/courses/modernizing-a-legacy-redux-application-with-react-hooks-c528), but redux hooks are only part of the story. They make it easy to connect your components with your redux store, but setting up and maintaining that store is still left up to you. [Redux Toolkit](https://redux-toolkit.js.org/) on the other hand provides some really nice guardrails and simplifies that process. It also includes utilities that improve TypeScript's ability to understand redux. This course follows the process of bringing redux in to manage the data in a shopping cart application. It focuses on splitting your store into slices, creating reducers and selectors and managing async actions with thunks. Our shopping cart app will allow you to see a dynamic list of products, add items to a cart, and eventually checkout. Follow along by creating this redux shopping cart application with me and then apply what you've learned to your own applications. Thank you so much for watching!
lessonDuration 1m 21sFree
The [payloadCreator](https://redux-toolkit.js.org/api/createAsyncThunk#payloadcreator) argument to `createAsyncThunk` takes two arguments. The first is a single `arg` that receives data passed into the thunk's action creator such as `dispatch(checkout(items))`. The second is called `thunkAPI` that contains something [like 7 different methods on it](https://redux-toolkit.js.org/api/createAsyncThunk#payloadcreator). Here we're going to use its `getState()` method to access the global state just before we make complete the checkout API call. When it comes to typing `createAsyncThunk` just like other RTK methods, it will mostly be typed by default. In this case we still need to tell it about our store. We can do that with `thunkAPI.getState() as RootState` which I think is the preferred way to handle the types here. If we really want to be thorough though, we can pass in the full types like this: ```ts createAsyncThunk< PayloadResponse, ThunkArgument, { state: RootState } >("slice/typeName", payloadCreator) ```
lessonDuration 1m 52sFree
This lesson uses the excellent [classnames](https://www.npmjs.com/package/classnames) library to apply different formatting to our checkout page based on the current state of our form. To model those states we use TypeScript's string literals, which are great for simple situations like this. This lesson sets us up nicely for responding to all the actions generated by our checkout thunk, which we'll explore in the next lesson. There's a great discussion here about when to use string literals over enums: https://stackoverflow.com/questions/49761972/difference-between-string-enums-and-string-literal-types-in-ts
lessonDuration 1m 51sFree
The Redux Style Guide explicitly recommends that you [avoid putting form state in redux](https://redux.js.org/style-guide/style-guide#avoid-putting-form-state-in-redux). Of course we do want to update our redux store with data that comes from user input at times, so how do we do it? Well, waiting until the form elements blurs is a good way. Otherwise you can imagine we'd be constantly dispatching redux actions and potentially re-rendering our whole app on every key press! The other thing I wanted to demonstrate from this lesson was using the `React.FocusEvent` type when handling form events. It's very common for people to say `e: any` and lose type support on event handlers, but I think it's much nicer to provide the correct types where possible. Once you get the hang of it, it's very easy. A full list of React Events can be found here: https://reactjs.org/docs/events.html#supported-events I also found the following resources to contain helpful information about typing react events: - https://felixgerschau.com/react-typescript-events/ - https://fettblog.eu/typescript-react/events/
lessonDuration 2m 5sFree
This course builds off a react-based shopping cart application containing a list of products (loaded dynamically from a JSON file). It is fully self-sustaining and does not rely on any third party APIs. It uses [vite](https://vitejs.dev/) as the underlying bundler and development tool with [react-ts](https://vitejs.dev/guide/#scaffolding-your-first-vite-project) preset. To get started simply follow the these instructions: ``` git clone git@github.com:xjamundx/redux-shopping-cart.git cd redux-shopping-cart git checkout egghead npm install npm run dev open http://localhost:3000 ``` **Note** This course uses a shopping cart application to demonstrate redux principles. It is not meant to demonstrate anything about making actual financial transactions over the web.
lessonDuration 4m 6s
In this lesson, we're going to explore template literal types, another powerful feature of TypeScript's type system. Template literal types have the same syntax as template literals in JavaScript, but they're used in type positions. Using template literal types, we can produce a union of string literal types and perform string concatenation in the type space: ```ts type Dimension = "block" | "inline"; type MarginProperty = `margin-${Dimension}`; ``` ## Additional Reading - [Template Literal Types](https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html) - [String Literal Types in TypeScript](https://mariusschulz.com/blog/string-literal-types-in-typescript) - [Mapped Types in TypeScript](https://mariusschulz.com/blog/mapped-types-in-typescript) - [`keyof` and Lookup Types in TypeScript](https://mariusschulz.com/blog/keyof-and-lookup-types-in-typescript)
lessonDuration 1m 15sFree
In this lesson, we learn how to use TypeScript's `Partial` types to make the `required` properties of your types optional; allowing you to reuse the same types for the cases where you may not have all the data.
lessonDuration 25s
In our [last lesson](https://egghead.io/lessons/javascript-use-partial-to-create-partially-fillable-types-in-typescript), we saw how we can make all the required fields in a type to be optional. Here we learn the opposite i.e. how to make all the optional fields to be required with the help of `Required`.
lessonDuration 55s
There might be cases where you have selective data for your entities. Let's say that you are building a public API endpoint to get all the registered users from your `users` collection. Now there might be sensitive data in your `User` entity type that you may not want to return in the response. In such cases, `Pick` can help you be selective and get only the properties you need. In this lesson, we will learn how to extract properties from a type and create a new type from it.
lessonDuration 42s
There might be cases where you would want to create a type while excluding some of the properties from a type. Let's say you have a database query on a `users` table where you are not selecting the `password` field. In this case, you will not be able to assign the query results to your `User` type because it is not going to have all the fields available. In this lesson, we are going to learn how we can use TypeScript's `Omit` utility type to exclude properties from a type.