Skip to content

Commit 663c37a

Browse files
committed
refactor: 타입 단언에서 타입 가드로 변경
1 parent 0e4f9da commit 663c37a

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/components/TodoForm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default class TodoForm {
2020
if (!this.isInit) {
2121
this.$form.addEventListener("submit", (e) => {
2222
e.preventDefault();
23-
const $todo = this.$form.querySelector("input[name=todo]") as HTMLInputElement;
23+
const $todo = this.$form.querySelector<HTMLInputElement>("input[name=todo]");
24+
if (!$todo) return;
2425
const text = $todo.value;
2526
if (text.length > 1) {
2627
$todo.value = "";

src/components/TodoList.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export default class TodoList {
2424

2525
if ($li) {
2626
const newState = [...this.state];
27-
const index = +($li.dataset.index as string);
27+
if (typeof $li.dataset.index !== "string") return;
28+
const index = +$li.dataset.index;
2829

2930
if (target.className === "deleteBtn") {
3031
newState.splice(index, 1);

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { getItem } from "./utils/storage.js";
33

44
const initialState = getItem("todo", []);
55
const initialCount = getItem("count", { total: 0, done: 0 });
6-
const $app = document.querySelector("#app") as HTMLElement;
6+
const $app = document.querySelector<HTMLElement>("#app");
77

8-
new App($app, initialState, initialCount);
8+
$app && new App($app, initialState, initialCount);

0 commit comments

Comments
 (0)