microsoft / microsoft/TypeScript

Unexpected type assertion result

Aperta
#32,653 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.6.0-dev.20190801

Search Terms:

  • type assertion absolute import
  • type assertion import
  • type cast absolute import
  • type cast import
  • type 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)?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Clona il repository di riproduzione collegato e compila i file sotto src/ con le dichiarazioni abilitate, quindi confronta dist/index.d.ts con l’output previsto. Inizia tracciando la generazione delle dichiarazioni per l’assertion in src/index.ts e il tipo Model da src/index.model.ts. Il lavoro è completo quando la dichiarazione generata conserva Model e usa un percorso di importazione che rimane valido da dist/.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.