The Good, The Bad & The Ugly
@sepehrity
February 2020
#iran_react
@sepehrity
@sepehrity
@sepehrity
@sepehrity
@sepehrity
@sepehrity
cannot read property # of null/undefined
@sepehrity
const getBar = () => {
const x = 'my string';
const y = x.foo;
return y.bar;
};
getBar();
// Cannot read property 'bar' of undefined
@sepehrity
@sepehrity
const getColor = (person) => {
if (person.age > 18) {
return 'green';
} else {
return 'red';
}
};
getColor(90);
// 'red'
@sepehrity
@sepehrity
@sepehrity
TypeScript is a typed superset of JavaScript that compiles
to plain JavaScript.
@sepehrity
@sepehrity
type States = {
loading: boolean;
users?: User[];
};
if (loading) return <Loading />
return (
<button onClick={handleClick}>show users</button>
)
@sepehrity
const handleClick = (event: React.MouseEvent<...>) => {
console.log(users.map(() => ...));
// Object is possibly 'null'
};
if (loading) return <Loading />
return (
<button onClick={handleClick}>show users</button>
)
@sepehrity
if (loading) return <Loading />
return (
<UsersList users={users} />
)
const UsersList = ({ users }) => {
const handleClick = (
event: React.MouseEvent<...>
) => {
console.log(users.map(() => ...)); // never be null
};
return (
<button onClick={handleClick}>show users</button>
)
}
@sepehrity
@sepehrity
@sepehrity
@sepehrity
@sepehrity
@sepehrity
Types First, Codes Next
Codes First, Types Next