microsoft / microsoft/TypeScript

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

Offen
#37,888 8 Kommentare 9 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@weswigham arbeitet bereits daran.

Seit 13.4.2020.

Needs Investigation Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.