typescript 8

23.02.05 왜 super call?

Constructors for derived classes must contain a super call super() 을 호출하지 않으면 위와 같은 타입스크립트 에러로 this 로 서브 클래스에 접근할 수 없다. (타스만의 에러는 아님.) 해주면 그럼 뭐 에러는 해결인데 왜 해줘야 하는지 좀 의문.. 서브 클래스에서 constructor 가 실행될 때, 생성자 함수에서는 슈퍼클래스에 빈 객체를 만들고 this 로 객체 할당해주라고 기다리고 있다. 그러니 서브 클래스 입장에서는 그냥 못 만들고 super() 을 호출해서 슈퍼 클래스의 constructor 를 실행해야 하는 것이다. 그러면 비로소 this 객체가 만들어지고 this. 를 사용할 수 있다~!~!

typescript 2023.02.05

23.01.29 Access Modifier

클래스에서 사용할 수 있는 access modifier 접근 제한자에는 3가지 종류가 있다. public, private, protected 접근 가능성 public protected private 클래스 내부 O O O 자식 클래스 내부 O O X 클래스 인스턴스 O X X 클래스 인스턴스를 통해 외부 참조가 가능한 것은 public 만 해당된다. class A { public a: string; protected b: string; private c: string; } const A = new A('a', 'b', 'c'); console.log('public 가능', A.a); 자식 클래스를 통해 내부 참조가 가능한 것은 public 과 protected 둘만 해당된다. class B extends..

typescript 2023.01.30

22.02.18 interface extends 안될 때, Omit

보통 타입스트립트의 type을 지정해줄 때 인터페이스를 정의한 후 그 안에 모든 타입을 넣어준다. 그런데 그 인터페이스에서 어떤 변수 하나의 데이터를 정제해서 타입을 다르게 다시 정의하고 싶은 경우나 인터페이스의 어떤 변수를 제외하고 다른 변수들을 추가하고 싶을 때는 전에 썼던 인터페이스 정의 코드를 다시 복붙해서 쓰기에는 하드코딩이라 좀 망설여진다... 만약에 이런 타입 정의 인터페이스가 있다고 하자. interface Data { id: number name: string place: string[] } place 데이터의 타입을 string[]에서 그냥 string 으로 바꿔서 다시 인터페이스 interface DataResult extends Data 처음에 나는 // 이렇게 하면 extends ..

typescript 2022.02.20

21.12.23 [typescript] setState props로 넘길 때 Dispatch

처음에 상위 컴포넌트에서 useState 상태 변경 함수를 넘길 때 그냥 void로 넘겼는데 정의할 수 있는 타입이 따로 있었다! interface Props{ isMade: boolean setIsMade: () => void } import React, { useState, Dispatch, SetStateAction } from 'react' interface Props { isMade: boolean setIsMade: Dispatch } 참고문서링크: https://newbedev.com/passing-usestate-as-props-in-typescript Programming tutorials | Newbedev Checkout new tutorials and guides for progra..

typescript 2021.12.23

21.12.22 형식의 인수는 'string' 형식의 매개 변수에 할당될 수 없습니다.ts(2345)

내가 객체 타입의 데이터를 매개변수로 넘겨줄 때 그 매개변수의 타입을 string으로 해놓고 넘겨주고 있어서 발생한 오류였다. 타입을 일일이 지정해서 넘겨주는 데이터와 전달하는 파라미터 사이의 데이터 타입이 일치해야 한다. 예를 들어서, 요로코롬 쓰고 있었다면 const data = { name: 'jemerald', age: 26, job: 'developer', } const onSubmit = (item: string) => { console.log('item', item) } 1. 데이터 자체를 바꾸겠다 (좀 비추) // 타입만 string으로 취급하는 객체 const data = "{ name: 'jemerald', age: 26, job: 'developer'}" const onSubmit =..

typescript 2021.12.23

21.12.12 'Element[]' 형식에 'ReactElement<any, any>' 형식의 type, props, key 속성이 없습니다.

원래는 AppContainer 하위에 map으로 돌린 컴포넌트밖에 없었는데 AppContainer 안에 map 돌린 컴포넌트 바로 위에 컴포넌트를 하나 더 추가했더니 다음과 같은 오류가 발생하였다. https://stackoverflow.com/questions/58311442/type-element-is-missing-the-following-properties-from-type-element-type Type 'Element[]' is missing the following properties from type 'Element': type, props, key I have the standard arrow map ES7 function with Typescript and React environmen..

typescript 2021.12.12