I'm not sure I want to encourage this, but to answer the OP's question literally:
You can put anything you want into global scope and then refer to it elsewhere.
For instance, you could put the following on your index.html equivalent:
function logit(x, loc) {
if (console && console.log) {
console.log(loc, JSON.stringify(x, null, ' '));
}
}
Now you can use this anywhere you want in one of at least two ways.
Kludge your global scope.
(window as any).logit(item, "some location")
Ewwww.
Use declare
You can also refer to it using declare
.
So in a regular TypeScript file:
declare const logit: (x: any, loc: string) => void;
// you could also just use
// declare const logit: any;
export default class MyClass {
public handleIt(x: string) {
logit(x, "MyClass.handleIt");
// ... logic
}
}
Note that any file, not just "index.html", is fair game as a launching point to push stuff into the global scope. Just remember that you could get into trouble if you were, say, lazy loading a module.
Your example
So for you, set those two values globally however you want (or maybe a non-TypeScript library does it for you, which would almost make this a valid use case), and then do something like...
declare let CANVAS_WIDTH: number; // maybe const, I don't know your use case.
declare let CANVAS_HEIGHT: number;
export class Bullet {
x: number = 22;
y: number = 22;
constructor (speed: number) {
this.xVelocity = speed;
}
inBounds() {
return this.x >= 0 && this.x <= CANVAS_WIDTH &&
this.y >= 0 && this.y <= CANVAS_HEIGHT;
};
}
But, again, this is highly anti-pattern, and probably indicates a code smell. You want to have static values on another class as Rajagopal 웃 suggests.