Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Allow importing types from within nested modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc committed Oct 18, 2018
1 parent a1c128a commit 93a8303
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
4 changes: 3 additions & 1 deletion examples/typescript-react-example/src/WrapJsValue.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions examples/typescript-react-example/src/WrapJsValue.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ let roundedNumber = round(1.8);
[@genType]
let areaValue = area({x: 3, y: None});

[@genType.import "./MyMath"]
[@genType.as "AbsoluteValue"]
type absoluteVaue = {. "getAbs": (. unit) => int};

/* This is untyped */
[@bs.send] external getProp: absoluteVaue => int = "getProp";

/* This is also untyped, as we "trust" the type declaration in absoluteVaue */
let getAbs = (x: absoluteVaue) => {
let getAbs = x##getAbs;
getAbs(.);
module AbsoluteValue = {
[@genType.import "./MyMath"]
[@genType.as "AbsoluteValue"]
type t = {. "getAbs": (. unit) => int};

/* This is untyped */
[@bs.send] external getProp: t => int = "getProp";

/* This is also untyped, as we "trust" the type declaration in absoluteVaue */
let getAbs = (x: t) => {
let getAbs = x##getAbs;
getAbs(.);
};
};

[@genType]
let useGetProp = (x: absoluteVaue) => x->getProp + 1;
let useGetProp = (x: AbsoluteValue.t) => x->AbsoluteValue.getProp + 1;

[@genType]
let useGetAbs = (x: absoluteVaue) => x->getAbs + 1;
let useGetAbs = (x: AbsoluteValue.t) => x->AbsoluteValue.getAbs + 1;
6 changes: 3 additions & 3 deletions examples/typescript-react-example/src/WrapJsValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const area: (_1:point) => number = function _(Arg1) { const result = area
// tslint:disable-next-line:no-var-requires
const WrapJsValueBS = require('./WrapJsValue.bs');

import {AbsoluteValue as absoluteVaue} from './MyMath';
import {AbsoluteValue as AbsoluteValue_t} from './MyMath';

// tslint:disable-next-line:interface-over-type-literal
export type point = {x: number, y?: number};
Expand All @@ -28,6 +28,6 @@ export const roundedNumber: number = WrapJsValueBS.roundedNumber;

export const areaValue: number = WrapJsValueBS.areaValue;

export const useGetProp: (_1:absoluteVaue) => number = WrapJsValueBS.useGetProp;
export const useGetProp: (_1:AbsoluteValue_t) => number = WrapJsValueBS.useGetProp;

export const useGetAbs: (_1:absoluteVaue) => number = WrapJsValueBS.useGetAbs;
export const useGetAbs: (_1:AbsoluteValue_t) => number = WrapJsValueBS.useGetAbs;
9 changes: 7 additions & 2 deletions src/Translation.re
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,21 @@ let translateTypeDeclaration =
|> Annotation.getAttributePayload(Annotation.tagIsGenTypeImport)
) {
| Some(StringPayload(importString)) =>
let nameWithModulePath =
dec.typ_id |> Ident.name |> TypeEnv.addModulePath(~typeEnv);
let (typeName, asTypeName) =
switch (
dec.typ_attributes
|> Annotation.getAttributePayload(Annotation.tagIsGenTypeAs)
) {
| Some(StringPayload(asString)) => (
asString,
Some(dec.typ_id |> Ident.name),
Some(nameWithModulePath),
)
| _ => (
nameWithModulePath,
None,
)
| _ => (Ident.name(dec.typ_id), None)
};

let codeItems = [
Expand Down

0 comments on commit 93a8303

Please sign in to comment.