Answer the question
In order to leave comments, you need to log in
Why is the wrong type returned?
Hello!
There is such an interface
export interface DocExportRequest {
id: string
type: 'PDF' | 'CSV'
[k: string]: any
}
// see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-414782407
// disables `[k: string]: any;` indexing
export type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U }
? U
: never
const config = {
async: isDevelopment,
useTypescriptIncrementalApi: true,
checkSyntacticErrors: true,
reportFiles: ['**', '!**/__tests__/**', '!**/?(*.)(spec|test).*', '!**/src/setupProxy.*', '!**/src/setupTests.*'],
silent: true,
};
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@assets/*": ["src/assets/*"],
"@src/*": ["src/*"]
},
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"typeRoots": [
"./node_modules/@types"
],
"types": [
"node",
"webpack-env",
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"jsx": "react"
},
"include": ["src", "public"]
}
type TDocExportReq = KnownKeys<DocExportRequest>
type TDocExportReq {
id: string
type: 'PDF' | 'CSV'
}
Answer the question
In order to leave comments, you need to log in
This is "broken" in new versions of typescript. https://github.com/microsoft/TypeScript/issues/44143
In versions ^4.2, you can use alternative solutions, for example
type KnownKeys<T> = keyof { [P in keyof T as
string extends P ? never : number extends P ? never : P
]: T[P]; };
type KeyToKeyNoIndex<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
};
type ValuesOf<T> = T extends { [K in keyof T]: infer U; } ? U : never;
export type KnownKeys<T> = ValuesOf<KeyToKeyNoIndex<T>>;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question