How to access Assets API from typescript?


#1

I cannot for my life figure out how to invoke api methods like Assets.

The typings for Assets declare it to live in “meteor/tools” but that package doesn’t seem to exist so that was a no go and I also couldn’t figure out how to write my own index.d.ts file to make Assets methods available.

Can anyone help out?

/Per


#2

Assets is global and doesn’t have an import path (because it loads very early in Meteor’s lifecycle), so you can only use it as a global.

Not 100% on the typescript part, but it sounds like you just have to tell typescript that it exists and give it a type?

declare var Assets: any;

#4

I could not get that to work - for some reason the Assets variable is not accessible for typescript code - when running the code it comes up as undefined also when accessing it in the js console.

This is what I tried (and failed)

declare module "meteor/meteor" {
  import * as tools from "meteor/tools";
  export var Assets: typeof tools.Assets;
}

#5

Which typescript package are you using? I’ll try it out


#6

I use adornis:typescript at version 0.9.9 (Typescript 3.4) and then in my using code

import { Meteor, Assets } from “meteor/meteor”


Assets.getAbsoluteFilePath(“asdfasdf”) <== cannot call getAbsoluteFilePath of undefined


#7

As I said in the first reply, there is no import for Assets, it’s a global, just use it directly.

To suppress the typescript warnings, you can add a type declaration like so:

declare var Assets: any;

import { Meteor } from “meteor/meteor”

…
Assets.getAbsoluteFilePath(“asdfasdf”)

I’ve tested this in adornis:typescript and barbatus:typescript