question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constants + Generic causing TS4023 ("but cannot be named")

See original GitHub issue

TypeScript 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
unionalcommented, Sep 21, 2020

I’m able to reproduce it without generics.

Repro here: https://github.com/cyberuni/typescript-37888

// types.ts
export const typeSym = 'type'

// null.ts
import { typeSym } from './type'

export const nil = { [typeSym]: 'null' as const }

// optional.ts
import { nil } from './null'

export const optional = { nil } // 'typeSym' cannot be named

I explain it in detail here:

image

2reactions
bluepichucommented, May 23, 2021

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

// a.ts
export const A = Symbol("A");

export function create() {
	return { [A]: true };
}

// b.ts
import { create } from "./a";
export const b = create(); // TS4023: Exported variable 'b' has or is using name 'A' from external module...

Workaround Pattern

// a.ts
export const A = Symbol("A");

export interface WithAField<T> {
	[A]: T;
}

export function create(): WithAField<true> {
	return { [A]: true };
}

// b.ts
import { create } from "./a";
export const b = create(); // Emits as import("./a").WithAField<true>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found