Comments (9)
Oh, after some recent changes you need to include the type parameter. So Proxy :: Proxy (D T)
or Proxy :: Proxy (HigherKind T)
. For multiple params you can do T1
, T2
, etc. Sorry, I need to update the docs.
from aeson-typescript.
Thanks. I noticed that this is the intended usage from the tests, but I was unsure since I didn't manage to get it working in my code (not the one mentioned above).
What I need is a higher kinded type parameter. e.g.
data X f = X (f Int)
Is this possible?
from aeson-typescript.
Yes, it's possible as long as when you call getTypeScriptDeclarations
you fill in f
with something such that you get a reasonable type to map to TypeScript. For example, see the tests in TypeFamilies.hs
under the heading "Complicated Beam-like user type".
Somewhat related to this, there is special built-in support for type families, when get mapped to TypeScript lookup types. You can also see that in the TypeFamilies.hs
tests, somewhat mixed together with the other higher kinded stuff. I need to write that up properly as well.
from aeson-typescript.
Could you write an simple example of what to pass to my X
type for f
argument in getTypeScriptDeclaratios? I tried making a type TtoT
similar to yours T
, but I didn't manage to get it to work.
from aeson-typescript.
data X f = X (f Int)
$(deriveTypeScript A.defaultOptions ''X)
-- Unfortunately we need to declare this funny instance because the constraints on
-- getTypeScriptDeclarations are a little too tight in this case
instance TypeScript (HashMap String) where
getTypeScriptType _ = "void"
decls = getTypeScriptDeclarations (Proxy :: Proxy (X (HashMap String)))
> putStrLn $ formatTSDeclarations decls
type X = IX;
type IX = {[k: string]: number};
from aeson-typescript.
So is this something like making and typescript type for a "subtype" Y of X where
type Y = X (HashMap String)
but HashMap String
is arbitrarily chosen and it could be any * -> *
type like Identity
or Maybe
as well?
As I understood, now the f
as a type argument doesn't exist in TS? So it's just like a type without an argument, but I got to chose the specification (HashMap String) in Proxy (X (HashMap String)))
it possible that f
remains unchosen so it remains a higher level type argument (or some hacky equivalent of it) in the TS?
from aeson-typescript.
My ultimate goal is to have this type in haskell:
data AlterList a b c d n = AlterList (a n) [(b n, c n)] (d n) (c n)
and have the TS equivalent without worrying to much about the TS (which I am not very familiar with)
from aeson-typescript.
The part where you want f
to remain unchosen sounds like you might want the closed type family stuff:
type family MyTypeFamily a = result | result -> a where
MyTypeFamily Int = String
MyTypeFamily String = Bool
MyTypeFamily T = Int
data X a = X (MyTypeFamily a)
$(deriveTypeScript' A.defaultOptions ''X (ExtraTypeScriptOptions { typeFamiliesToMapToTypeScript = [''MyTypeFamily] }))
decls = getTypeScriptDeclarationsRecursively (Proxy :: Proxy (X T))
> putStrLn $ formatTSDeclarations decls
interface MyTypeFamily {
number: string;
string: boolean;
T: number;
}
type IX<T extends keyof MyTypeFamily> = MyTypeFamily[T];
type X<T extends keyof MyTypeFamily> = IX<T>;
In general, TypeScript has a less powerful type system than Haskell, so it's hard to map your AlterList
to TS generally, without knowing more about what a, b, c, d can be. But presumably you're also coming up with FromJSON
/ToJSON
instances, so that might help narrow down the types. (For example, a n
couldn't be a function type.)
from aeson-typescript.
I updated the docs so I'm going to close this now, but feel free to let me know if you have any other questions.
from aeson-typescript.
Related Issues (18)
- Extend the tests to cover all combinations of Aeson encoding options HOT 1
- Handwritten TypeScript instances for higher-order ADTs HOT 2
- [RFC] - Format Sum types HOT 4
- Stackage nightly test failure HOT 2
- Fails to build with GHC 9.0.1 HOT 1
- Always generate "tagged" unions HOT 2
- Extend Map instance to handle `ToJSONKeyValue` keys HOT 5
- Deriving TypeScript fails for a higher-kinded, indirectly-recursive type HOT 2
- Add TypeScript instance for UTCTime HOT 3
- Haskell types with a `'` in the name result in broken code HOT 1
- Include Haddock documentation in generated types HOT 1
- Cut a new version to fix GHC 9.6 compilation errors HOT 2
- Prohibit `undefined` in TypeScript Index Signatures for `Data.Map` when key is `String` HOT 3
- Generate user-defined type guards HOT 6
- [Bug] `formatTSDeclarations'` does not honor `typeAlternativesFormat` if there is more than 1 `TSDeclaration` in the input list HOT 8
- `interfaceNameModifier` applied to tail of interface name not whole interface name, triggers name format error for valid interface names
- Writing custom TypeScript instances HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aeson-typescript.