I'm Learning TypeScript!
Here's what I learned so far using it in Gatsby

I am a full-stack web developer that enjoys coding in Laravel and React.
Search for a command to run...
Here's what I learned so far using it in Gatsby

I am a full-stack web developer that enjoys coding in Laravel and React.
No comments yet. Be the first to comment.
In this series I'll discuss how I solved problems with frontend technologies like React, Vue, Next, Nuxt, Gatsby, etc. in the style of tutorials.
If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...
Switching stacks

If you don't know what Twin Macro is, check it out! It's a great library that blends Tailwind CSS and Styled Component systems, like Emotion. I love using Twin because it lets me use Tailwind while separating my styling from my markup and methods. If...

Many developers know that full-stack apps with a Vue frontend can be quickly spun up with Laravel Jetstream. What many don’t know is that recently, the Laravel team made it easy to make an Inertia app with Laravel Breeze. In this article, we'll make ...

Extending the Rule Object with a DateTime Trait

Using Custom Console Commands

I finally gave in! After seeing a (sometimes annoying) barrage of tweets and posts urging developers to switch to TypeScript, I decided to just give it a shot. And you know what, I like it! I prefer the backend more, so I freeze up when I need to learn frontend stuff. But since last year I realized that I do love working with JavaScript frameworks like Gatsby, so learning TypeScript always felt inevitable. So, below I've written some stuff I've learned so far. This is not a tutorial, but there are some things below that could help you.
Preliminaries:
npm install -g typescript.npm install --save @types/react @types/react-dom.compilerOptions I can override certain configurations for TypeScript's compiler. For example, I can compile my TypeScript down to an older version of ECMAScript with target. {
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"jsx": "react",
"lib": ["dom", "es2015", "es2017"],
"strict": true,
"noEmit": true,
},
}
The Learning Begins: TypeScript offers all the features of JavaScript with the type system layered on top. This is why they call TypeScript a superset of JavaScript. TypeScript helps prevent weird things in JavaScript like below from happening.

boolean, number, and string. They have a special type called any too.? operator after the property name so that TypeScript won't scream at us if we don't use it down the line.number and string by typing id: number | string.type Pet = {
name: string
species: string
age: number
shots: boolean
}
function checkPet(pet: Pet){
...
console.log(pet.age) // will be a number
}
interface Pet {
name: string
species: string
age: number
shots: boolean
}
interface Dog extends Pet {
breed: string
}
function checkPet(dog: Dog){
...
console.log(dog.name) // will be a string
console.log(dog.breed) // will be a string
}
The Learning Continues: For my project I am using Twin Macro, which is basically a way to implement TailwindCSS into React. I'm using it with Emotion, the well-known React package to implement styled-components (CSS in JS).
title. Some other props like description, lang, and author won't be used much, so I knew I had to make a type where I could make those props optional.interface SEOProps {
description?: string,
lang?: string,
title: string,
author?: string
}
const SEO = ({description, lang, author, title}: SEOProps) => {
...
}
import React from 'react'
const Layout = ({children}: any) => (
...
)
The Learning Still Continues:
FunctionComponent type from React so that I could give a type to a component. Also, when I defined my type, I used an array as the property because I used the spread operator for my props. I'm struggling to describe this in words, so let me show you:import React, {FC} from 'react'
import tw from 'twin.macro'
interface ComponentProps {
[key: string]: any
}
const ThisButton: FC<ComponentProps> = props => <button {...props} />
export const Button = tw(ThisButton)`
bg-blue-700 hover:bg-blue-900
`
Conclusion: