타입 단언(aslias)과 선언 (as)
if (!this.formElement) {
throw new Error('form element not exist');
}
const $ = <T extends HTMLElement>(selector: string, target?: HTMLElement) => {
let $element = null;
if (target === undefined) {
$element = document.querySelector(selector);
} else {
$element = target.querySelector(selector);
}
if ($element === null) throw new Error('DOM is NOT EXISTS');
return $element as T;
};
(product:Product) => void
type CoinKey = 500 | 100 | 50 | 10;
interface CoinValueProps {
text: string;
value: CoinKey;
count: number;
}
const COINS_TYPES: Record<CoinKey, CoinValueProps> = {
500: {
text: '500원',
value: 500,
count: 0,
},
100: {
text: '100',
value: 100,
count: 0,
},
50: {
text: '50원',
value: 50,
count: 0,
},
10: {
text: '10원',
value: 10,
count: 0,
},
};
const TEST_KEY = {
500: 500,
100: 100,
50: 50,
10: 10,
};
const je = <CoinKey>TEST_KEY[500];
const j = COINS_TYPES[je];
const $ = <T extends HTMLElement>(selector: string) => {
const $element = document.querySelector(selector);
if ($element === null) throw new Error('DOM is NOT EXISTS');
return $element as T;
};
const $j = $<HTMLInputElement>('j');
const $j2 = $<HTMLParagraphElement>('j');
const $j3 = $<HTMLAnchorElement>('j');
const $$ = (selector: string) => document.querySelector(selector);
const $$j = $$('j') as HTMLInputElement;
const $$j2 = $$('j') as HTMLParagraphElement;
const $$j3 = $$('j') as HTMLAnchorElement;
export default {};
{target} : Mouseevnet & {target: HTMLElement}
Enforcing the type of the indexed members of a Typescript object?
interface CoinValueProps{
text: string;
value:CoinKeys;
}
interface CointProps{
[key: string] : CoinValueProps;
}