Skip to content

Instantly share code, notes, and snippets.

@ariutta
Last active August 29, 2015 14:16
Show Gist options
  • Save ariutta/109a3d8a772dc503e40f to your computer and use it in GitHub Desktop.
Save ariutta/109a3d8a772dc503e40f to your computer and use it in GitHub Desktop.
JSON-LD and TypeScript experiment.

TypeScript and JSON-LD now both have type information, with TypeScript describing program APIs and JSON-LD describing Linked Open Data. Could TypeScript and JSON-LD work together to make these two play well with each other? This example is a start for exploring what that could look like. Possible applications:

  • Notifications of type incompatibilities between a program and a REST API
  • Code completion for editors/IDEs
  • Documentation generation for both program and REST APIs
  • ...

And now that AtScript and TypeScript have merged, it would be cool to see OData and JSON-LD work together for a similar merge in the future.

The JSON-LD is based on this example.

Live transpiler from TypeScript to ES5: here.

$traceurRuntime.options.symbols = true;
$traceurRuntime.ModuleStore.getAnonymousModule(function(require) {
"use strict";
var ContextElement = function ContextElement() {};
($traceurRuntime.createClass)(ContextElement, {}, {});
var Context = function Context(context) {
this.validate = function() {};
};
($traceurRuntime.createClass)(Context, {}, {});
var Vehicle = function Vehicle($__1) {
var string = $__1.propulsion;
this.propulsion = propulsion;
};
($traceurRuntime.createClass)(Vehicle, {}, {});
Object.defineProperty(Vehicle, "annotations", {get: function() {
return [new Context('http://schema.org/Vehicle')];
}});
var Propulsion = function Propulsion() {};
($traceurRuntime.createClass)(Propulsion, {}, {});
Object.defineProperty(Propulsion, "annotations", {get: function() {
return [new Context('http://purl.org/vocabularies/princeton/wn30/propulsion')];
}});
var Color = function Color() {};
($traceurRuntime.createClass)(Color, {}, {});
Object.defineProperty(Color, "annotations", {get: function() {
return [new Context('http://schema.org/color')];
}});
var TeslaRoadster = function TeslaRoadster($__1) {
var Color = $__1.color;
this.color = color;
};
($traceurRuntime.createClass)(TeslaRoadster, {}, {});
Object.defineProperty(TeslaRoadster, "annotations", {get: function() {
return [new Vehicle({propulsion: 'electric'})];
}});
return {};
});
//# sourceURL=traceured.js
{
"@context": {
"gr": "http://purl.org/goodrelations/v1#",
"pto": "http://www.productontology.org/id/",
"foaf": "http://xmlns.com/foaf/0.1/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"foaf:page": {
"@type": "@id"
},
"gr:acceptedPaymentMethods": {
"@type": "@id"
},
"gr:hasBusinessFunction": {
"@type": "@id"
},
"gr:hasCurrencyValue": {
"@type": "xsd:float"
}
},
"@id": "http://example.org/cars/for-sale#tesla",
"@type": "gr:Offering",
"gr:name": "Used Tesla Roadster",
"gr:description": "Need to sell fast and furiously",
"gr:hasBusinessFunction": "gr:Sell",
"gr:acceptedPaymentMethods": "gr:Cash",
"gr:hasPriceSpecification": {
"gr:hasCurrencyValue": "85000",
"gr:hasCurrency": "USD"
},
"gr:includes": {
"@type": [
"gr:Individual",
"pto:Vehicle"
],
"gr:name": "Tesla Roadster",
"foaf:page": "http://www.teslamotors.com/roadster"
}
}
// Options: --annotations --array-comprehension --async-functions --async-generators --exponentiation --for-on --generator-comprehension --member-variables --proper-tail-calls --require --symbols --types
class ContextElement {
contextElement:string|object;
}
class Context {
context:ContextElement|Array<ContextElement>;
constructor(context) {
// add a function to validate that the JSON-LD
// context and the type annotations in this
// document agree.
this.validate = function() {};
}
}
@Context('http://schema.org/Vehicle')
class Vehicle {
propulsion:Propulsion;
constructor({propulsion:string}) {
this.propulsion = propulsion;
}
}
@Context('http://purl.org/vocabularies/princeton/wn30/propulsion')
class Propulsion {
type:string;
}
@Context('http://schema.org/color')
class Color {
hex:string;
}
@Vehicle({propulsion: 'electric'})
class TeslaRoadster {
color:Color;
constructor({color:Color}) {
this.color = color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment