TypeScript
자주 쓰이는 마스킹(정규식) 방법
자주 쓰이는 마스킹(정규식) 방법
2022.08.12e-commerce 및 회원정보 등에서 자주 사용하는 마스킹 방법 (정규식) 1. 천 단위 콤마( , ) 추가하기 const addComma = (num: number) => { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); }; console.log('addComma - result : ', addComma(123456789)); 2. 휴대폰 번호 하이픈( - ) 추가하기 const addHyphen = (phoneNum: String) => { if (!phoneNum) return ''; return phoneNum.replace(/^(\d{2,3})(\d{3,4})(\d{4})$/, `$1-$2-$3`); }; console.log('..
TypeScript - 타입 별칭(Type Alias)
TypeScript - 타입 별칭(Type Alias)
2022.02.07타입 별칭(Type Alias) Interface랑 비슷해 보인다. Primitive, Union Type, Tuple, Function, 기타 직접 작성해야하는 타입을 다른 이름을 지정할 수 있습니다. 만들어진 타입의 refer로 사용하는 것이지 타입을 만드는것은 아니다. Aliasing Primitive type MyStringType = string; const str = 'world'; let myStr: MyStringType = 'hello'; myStr = str; // 별 의미가 없다. Aliasing Union Type let person: string | number = 0; person = 'Kdong'; type StringOrNumber = string | number; let a..
TypeScript - 타입 호환성(Type Compatibility)
TypeScript - 타입 호환성(Type Compatibility)
2022.02.07타입 호환성(Type Compatibility) 서브 타입(1) // sub1 타입은 sup1 타입의 서브 타입이다. let sub1: 1 = 1; let sup1: number = sub1; sub1 = sup1; // error! Type 'number' is not assignable to type '1'. // sub2 타입은 sup2 타입의 서브 타입이다. let sub2: number[] = [1]; let sup: object = sub2; sub2 = sup2; // error! Type '{}' is missing the following properties from type 'number[]': length, pop, push, concat, and 16 more. // sub3 타입은..
TypeScript - Structural Type System VS Nominal Type System
TypeScript - Structural Type System VS Nominal Type System
2022.02.07Structural Type System VS Nominal Type System 타입스크립트(Structural Type System)의 타입 체계를 이해하는데 도움이 되느 시스템이다. interface IPerson { name: string; age: number; speak(): string; } type PersonType = { name: string; age: number; speak(): string; }; let personInterface: IPerson = {} as any; let personType: PersonType = {} as any; personInterface = personType; personType = personInterface; Structural Type Sy..
TypeScript - 작성자와 사용자의 관점으로 코드 바라보기
TypeScript - 작성자와 사용자의 관점으로 코드 바라보기
2022.02.07작성자와 사용자의 관점으로 코드 바라보기 타입 시스템 컴파일러에게 사용하는 타입을 명시적으로 지정하는 시스템 컴파일러가 자동으로 타입을 추론하는 시스템 타입스크립트의 타입 시스템 타입을 명시적으로 지정할 수 있다. 타입을 명시적으로 지정하지 않으면, 타입스크립트 컴파일러가 자동으로 타입을 추론한다. 사용자, 작성자의 입장 타입이란? 해당 변수가 할 수 있는 일을 결정한다 // JavaScript // f1이라는 함수의 bodt 에서는 a 를 사용할 것이다. // a 가 할 수 있는 일은 a 의 타입이 결정한다. function f1(a) { return a; } 자바스크립트에선 함수 사용법에 대한 오해를 야기할 수 있다. // JavaScript // (f2 실행의 결과가 NaN을 의도한 것이 아니라면)..
TypeScript - void
TypeScript - void
2022.02.07Void function returnVoid(message: string): void { console.log(message); return undefined; // function returnVoid(message: string): void // undefined 만 유일하게 void에 할당될 수 있다. } const r = returnVoid("리턴이 없다."); // const r: void // void 라고 표시된 함수의 리턴을 가지고 어떠한것도 하지 않겠다고 명시적으로 표현하는 것이다. Notion : https://torpid-pasta-de7.notion.site/Basic-Types-7c1eff4fb5f3449e932fb1d157da1f25
TypeScript - never
TypeScript - never
2022.02.07Never function error(message: string): never { throw new Error(message); } function fali() { return error("failed"); } function infinitLoop(): never { while (true) {} } never 타입은 모든 타입의 subtype 이며, 모든 타입에 할당할 수 있다. 하지만, never 에는 그 어떤 것도 할당할 수 없다. any 조차도 never에게 할당할 수 없다. 잘못된 타입을 넣는 실수를 막고자 할 때 사용하기도 한다. let a: string = "Kdong"; if (typeof a !== 'string') { a; // let a: never } declare const b: ..
TypeScript - unknown
TypeScript - unknown
2022.02.07Unknown 응용프로그램을 작성할 때 모르는 변수의 타입을 묘사해야 할 수도 있다. 이러한 값은 동적 콘텐츠(예: 사용자로부터, 또는 우리 API의 모든 값을 의도적으로 수락하기를 원할 수 있다.) 컴파일러와 미래의 코드를 읽는 사람에게 이 변수가 무엇이든 될 수 있음을 알려주는 타입을 제공하기를 원하므로 unknown 타입을 제공한다. declare const maybe: unknown; const aNumber: number = maybe; // Type 'unknown' is not assignable to type 'number'. if (maybe === true) { const aBoolean: boolean = maybe; // const aString: string = maybe; // ..