Skip to content

Commit 3d8096c

Browse files
committed
Fix Postcode Type Definitions to Optional in window
1 parent ae30f91 commit 3d8096c

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/DaumPostcode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component, createRef, CSSProperties } from 'react';
2-
import loadPostcode, { PostcodeOptions } from './loadPostcode';
2+
import loadPostcode, { PostcodeConstructor, PostcodeOptions } from './loadPostcode';
33

44
export interface DaumPostcodeProps extends Omit<PostcodeOptions, 'oncomplete' | 'onresize' | 'onclose' | 'onsearch'> {
55
onComplete?: PostcodeOptions['oncomplete'];
@@ -48,7 +48,7 @@ class DaumPostcode extends Component<DaumPostcodeProps, State> {
4848
loadPostcode(scriptUrl).then(initiate).catch(onError);
4949
}
5050

51-
initiate = (Postcode: typeof window.daum.Postcode) => {
51+
initiate = (Postcode: PostcodeConstructor) => {
5252
if (!this.wrap.current) return;
5353
const {
5454
scriptUrl,

src/loadPostcode.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
declare global {
22
interface Window {
3-
daum: {
4-
postcode: {
3+
daum?: {
4+
postcode?: {
55
load: (fn: () => void) => void;
66
version: string;
77
_validParam_: boolean;
88
};
9-
Postcode: {
10-
new (postcodeOptions: PostcodeOptions): Postcode;
11-
};
9+
Postcode?: PostcodeConstructor;
1210
};
1311
}
1412
}
@@ -114,27 +112,30 @@ export interface EmbedOptions {
114112
autoClose?: boolean;
115113
}
116114

115+
export interface PostcodeConstructor {
116+
new (postcodeOptions: PostcodeOptions): Postcode;
117+
}
118+
117119
export interface Postcode {
118120
open(openOptions?: OpenOptions): void;
119121
embed(element: HTMLElement, embedOptions?: EmbedOptions): void;
120122
}
121123

122124
const loadPostcode = (function () {
123125
const scriptId = 'daum_postcode_script';
124-
let promise: Promise<typeof window.daum.Postcode> | null = null;
126+
let promise: Promise<PostcodeConstructor> | null = null;
125127

126-
return function (url: string): Promise<typeof window.daum.Postcode> {
128+
return function (url: string): Promise<PostcodeConstructor> {
127129
if( promise ) return promise;
128130

129131
promise = new Promise((resolve, reject) => {
130132
const script = document.createElement('script');
131133
script.src = url;
132134
script.onload = () => {
133-
try {
134-
resolve(window.daum.Postcode);
135-
} catch (e) {
136-
reject(e);
135+
if( window?.daum?.Postcode ) {
136+
return resolve(window.daum.Postcode);
137137
}
138+
reject(new Error('Script is loaded successfully, but cannot find Postcode module. Check your scriptURL property.'))
138139
};
139140
script.onerror = (error) => reject(error);
140141
script.id = scriptId;

0 commit comments

Comments
 (0)