Constants + Generic causing TS4023 ("but cannot be named")
See original GitHub issueTypeScript Version: 3.8, but also the 3.9 beta
Search Terms: ts4023 cannot be named constant
Code
Create a tsconfig with declaration: true
. Then, in main.ts
:
import {StructType} from './lib';
declare function foo<T>(): T;
export const exportedFooFunc = foo<StructType>();
In lib.ts
:
export const SOME_CONSTANT = 'fieldKey';
export type MakeStruct<S> = S;
export type StructType = MakeStruct<{
readonly [SOME_CONSTANT]: any;
}>;
Old instructions (don't repro anymore)
Create a tsconfig with declaration: true
. Then, in main.ts
:
import {
ReturnType1,
ReturnType2,
} from './lib';
export const impl1 = (): ReturnType1 => ({
});
export const impl2 = (): ReturnType2 => ({
});
In lib.ts
:
export const SOME_CONSTANT = 'myConstant';
export type WrapType<S> = S;
export type ReturnType1 = WrapType<{
[SOME_CONSTANT]?: {};
}>;
export type ReturnType2 = {
[SOME_CONSTANT]?: {};
};
Expected behavior:
The file should be properly compiled.
Actual behavior:
TypeScript reports a diagnostic:
Exported variable 'exportedFooFunc' has or is using name 'SOME_CONSTANT' from external module "/.../lib" but cannot be named.
Playground Link: n/a
Related Issues: n/a
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
TS4023: Exported Variable <x> has or is using name <y> from ...
But there isn't a name in scope that refers directly to [Route], so the type "cannot be named" and there's an error.
Read more >TypeScript errors and how to fix them
error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. Broken...
Read more >mobx-state-tree - npm
mobx-state-tree is a state container that combines the simplicity and ease of mutable data with the traceability of immutable data and the ...
Read more >Kent C. Dodds on Twitter: "@pelotom I did see that same ...
Hey TypeScript friends I have a use case where a type I'm making needs to be a combination of a type from another...
Read more >Vol. 76 Tuesday, No. 177 September 13, 2011 Pages 56277 ...
(2) For service information identified in ... identified to be the root cause of the pin to ... agency may not conduct or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m able to reproduce it without generics.
Repro here: https://github.com/cyberuni/typescript-37888
I explain it in detail here:
Workaround to the “symbol-as-key” variant of this issue: if you give TS some non-
unique symbol
way of naming the type, it seems to work out ok. It’s a bit clunky since you have to explicitly type the result, but at least it compiles!Failing Pattern
Workaround Pattern