Repository navigation
Unexpected type assertion result #32653
Description
Activity
dragomirtitian commented
on Aug 1, 2019 ContributorMore actionsA generally feature of type aliases is that they can be expanded transparently by the compiler, so probably when you use the assertion the compiler does not think it needs to keep the name around.
Using an interface will probably get you the effect you desire:
export interface Model extends Pick<DType, keyof Exclude<DType, '__private'>> {}
A generally feature of type aliases is that they can be expanded transparently by the compiler, so probably when you use the assertion the compiler does not think it needs to keep the name around.
Using an interface will probably get you the effect you desire:
export interface Model extends Pick<DType, keyof Exclude<DType, '__private'>> {}
Yes, that works 👍
However, we cannot force people to use interfaces, therefore issue might still pop out causing frustrations, since it is not really straight forward problem.
Is there any other way?
EDIT
Furthermore, if types are generated by 3rd party, we are even out of control, and using the type directly from
generated/typeswill result in the same problem.I have update the repository accordingly:
export class A { value1 = <Model>{}; value2 = <Test['e']>{}; }
Results in
export declare class A { value1: Pick<{ __private: "d"; } & import("../generated/types").TypeD, "__private" | "da" | "db" | "dc">; value2: { __private: "e"; } & import("../generated/types").TypeD; }
- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.and removedBugA bug in TypeScriptA bug in TypeScript
on Jan 24, 2020
TypeScript Version: 3.6.0-dev.20190801
Search Terms:
type assertion absolute importtype assertion importtype cast absolute importtype cast importtype assertion pickCode
Repo: https://github.com/klemenoslaj/typescript-type-assertion
generated/types.d.tssrc/index.model.tssrc/index.tsdist/index.model.d.tsdist/index.d.tsExpected behavior:
I would expect that
dist/index.d.tstype definitions would result in:In fact, this is the result of type definitions, if I change
value = <Model>{};tovalue: Model = <Model>{};.Why doesn't typescript use
Modeldirectly when type asserting?Actual behavior:
Results in:
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 thesrc/and not fromdist/, effectively producing an error when projects start using one another.Why doesn't typescript:
generated/types)?Model)?