microsoft / microsoft/TypeScript
Unexpected type assertion result
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: 3.6.0-dev.20190801
Search Terms:
type assertion absolute importtype assertion importtype cast absolute importtype cast importtype assertion pick
Code
Repo: https://github.com/klemenoslaj/typescript-type-assertion
generated/types.d.ts
export interface TypeD {
da: string;
db: string;
dc: string;
}
export interface Test {
a: string;
b: string;
c: string;
d?: { __private: 'd' } & TypeD;
}
src/index.model.ts
import { Test } from 'generated/types';
type DType = NonNullable<Test['d']>;
export type Model = Pick<DType, keyof Exclude<DType, '__private'>>;
src/index.ts
import { Model } from './index.model';
export class A {
value = <Model>{};
}
dist/index.model.d.ts
import { Test } from 'generated/types';
declare type DType = NonNullable<Test['d']>;
export declare type Model = Pick<DType, keyof Exclude<DType, '__private'>>;
export {};
dist/index.d.ts
export declare class A {
value: Pick<{
__private: "d";
} & import("../generated/types").TypeD, "__private" | "da" | "db" | "dc">;
}
Expected behavior:
I would expect that dist/index.d.ts type definitions would result in:
import { Model } from './index.model';
export declare class A {
value: Model;
}
In fact, this is the result of type definitions, if I change value = <Model>{}; to value: Model = <Model>{};.
Why doesn't typescript use Model directly when type asserting?
Actual behavior:
Results in:
export declare class A {
value: Pick<{
__private: "d";
} & import("../generated/types").TypeD, "__private" | "da" | "db" | "dc">;
}
Playground Link:
I couldn't figure out how to compile declarations in playground, sorry :(
As an alternative please clone the following repo: https://github.com/klemenoslaj/typescript-type-assertion
Explanation:
This is quite a significant problem in monorepo environment.
What happens is that projects are compiled in the dist/ folder and they contain relative path like above (import("../generated/types")), which points from the src/ and not from dist/, effectively producing an error when projects start using one another.
Why doesn't typescript:
- use the original path from the import (
generated/types)? - use the type from assertion directly (
Model)?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Klone das verknüpfte Repro-Repository und kompiliere die Dateien unter src/ mit aktivierten Deklarationen. Vergleiche anschließend dist/index.d.ts mit der erwarteten Ausgabe. Beginne damit, die Deklarierungsgenerierung für die Assertion in src/index.ts und den Model-Typ aus src/index.model.ts nachzuverfolgen. Fertig ist es, wenn die generierte Deklaration Model beibehält und einen Importpfad verwendet, der von dist/ aus weiterhin gültig ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100