Why doesn't TypeScript report an error when an unexpected field is provided in an interface where all keys are optional? Here is the code snippet:
// This is an interface which all the key is optional
interface AxiosRequestConfig {
url?: string;
method?: string;
baseURL?: string;
headers?: any;
params?: any;
paramsSerializer?: (params: any) => string;
data?: any;
timeout?: number;
timeoutErrorMessage?: string;
withCredentials?: boolean;
responseType?: ResponseType;
xsrfCookieName?: string;
xsrfHeaderName?: string;
onUploadProgress?: (progressEvent: any) => void;
onDownloadProgress?: (progressEvent: any) => void;
maxContentLength?: number;
validateStatus?: ((status: number) => boolean) | null;
maxBodyLength?: number;
maxRedirects?: number;
socketPath?: string | null;
httpAgent?: any;
httpsAgent?: any;
}
"a" field is unexpected, why TypeScript does not report an error ?
type test= {
[key:string]: (...args:any[])=> AxiosRequestConfig
}
const serviceMap: test = {
getOperationLogListByConditions(conditions: any) {
const {
currentPageIndex,
createTimeStart,
createTimeEnd,
...rest
} = conditions
return {
url: '/operateLog/page',
method: 'GET',
params: {
...rest,
pageNo: currentPageIndex,
createTimeStart: encodeURIComponent(createTimeStart),
createTimeEnd: encodeURIComponent(createTimeEnd)
},
a:"" // "a" field is unexpected, why TypeScript does not report an error ?
}
}
I am unsure why this is happening, please provide some insight! The TypeScript version being used is 4.1.2.