-
Notifications
You must be signed in to change notification settings - Fork 90
introduced model interface IPerson #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import React, { useState } from 'react' | ||
| import { IState as Props } from "../App"; | ||
| import {IPerson} from "../models/IPerson"; | ||
|
|
||
|
|
||
| interface IProps { | ||
| setPeople: React.Dispatch<React.SetStateAction<Props["people"]>> | ||
| people: Props["people"] | ||
| setPeople: React.Dispatch<React.SetStateAction<IPerson[]>> | ||
| people: IPerson[] | ||
| } | ||
|
|
||
| const AddToList: React.FC<IProps> = ({setPeople, people}) => { | ||
|
|
@@ -32,7 +33,7 @@ const AddToList: React.FC<IProps> = ({setPeople, people}) => { | |
| age: parseInt(input.age), | ||
| img: input.img, | ||
| note: input.note | ||
| } | ||
| } as IPerson | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid type assertions if we can. It basically tells typescript "I know more than you do about this type", with the risk of being wrong 😄 . We shouldn't need to tell typescript about the type of this object, as the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as a dotnetter I always shudder when I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is not really such thing in typescript due to structural typing. If an object has the same shape as the But when you create a variable and want it to explicitly be of type const person: IPerson = {
name: "Marc",
age: 1,
img: "foo"
} |
||
| ]); | ||
|
|
||
| setInput({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export interface IPerson { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would agree, the more we are using this type across our codebase, the more it makes sense to extract it to a separate location. I totally agree with the coupling argument. The The question is rather, should the One could argue, that as a presentational component, it defines its own type. It renders four input fields no matter how the On the other hand, one could say that in this specific app, it makes sense that whenever we change the I hope this made sense 😅 |
||
| name: string; | ||
| age: number; | ||
| img: string; | ||
| note?: string; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
React.Dispatch<React.SetStateAction<>>already introduces coupling as we are basically saying that it must be a native react state setter the needs to be passed in here. But what if we are not using react state in the parent component?We could, instead, define what the
AddToListcomponent actually expects: A function that takes a list of persons and returns nothing: