mike-north / jsonapi-typescript

TypeScript type information for JSON:API documents
52 stars 5 forks source link

Importable types? #3

Closed davewasmer closed 6 years ago

davewasmer commented 6 years ago

Just curious if there is a way to use these types via direct import rather than relying on typeRoots and global namespaces? Something like:

import JSONAPI from 'jsonapi.ts';

The above doesn't work because Typescript doesn't allow for imports ending in .ts;

mike-north commented 6 years ago

Does it work if this package's name changes to something without .ts in it (i.e., jsonapi-ts)?

davewasmer commented 6 years ago

I think we'd also have to change it so it didn't use namespace, but instead simply exported everything that's top level in the current namespace as a top level export of the module itself. Then you'd do something like this to get the namespace-like references:

import * as JSONAPI from 'jsonapi-ts';

Or if you wanted only one interface:

import { Meta as JSONAPIMeta } from 'jsonapi-ts';
mike-north commented 6 years ago

AFAIK nobody depends on this yet except me, so I'm willing to change it around. I like this a bit better than the current setup anyway, and we can use it for @types/ember-data more easily too.

mike-north commented 6 years ago

Not sure if I'll get to it this week (tests will have to be adjusted, etc...) but we'll have to treat https://github.com/mike-north/json-typescript the same way. Here's a quick and dirty index.ts that works for me

// tslint:disable-next-line:array-type
type Arr<T> = Array<T>;
// tslint:disable-next-line:no-namespace
namespace JSON {
    export type Primitive = string | number | boolean | null;
    export interface Object {
        [member: string]: Value;
    }
    export interface Array extends Arr<Value> {}
    // tslint:disable-next-line:array-type
    export type Value = Primitive | Object | Array;
}
export = JSON;
davewasmer commented 6 years ago

Awesome - happy to make some PRs here too. I basically have a less-complete version of this in https://github.com/denali-js/denali/blob/master/lib/render/json-api.ts right now, and would love to extract / off-load to a separate package since it seems potentially useful for Denali consumers as well as others.

Do you want to stick with using namespace for now? From the little I've seen, it seems like namespaces tend to eschewed in favor of modules in Typescript. I'm basing that off very little data and gut feels mostly though, so happy to stick with 'em if you'd prefer.

For reference, here's how I'd imagine your snippet above done via modules:

// tslint:disable-next-line:array-type
type Arr<T> = Array<T>;
export type Primitive = string | number | boolean | null;
export interface Object {
    [member: string]: Value;
}
export interface Array extends Arr<Value> {}
// tslint:disable-next-line:array-type
export type Value = Primitive | Object | Array;
}
mike-north commented 6 years ago

yeah, a namespace-free implementation would be fine, as long as namespace imports continue to work