-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make extensible & externally usable #1
Comments
@OlsonDev Hey, sorry for the delay. Been getting through all the issues on my backlog. These types of interfaces are tricky to type - let me see how we can do this. |
@blakeembrey No problem! 😄 |
@OlsonDev Honestly, I just spent 25 minutes looking at it and I can't think of how to make this interface work. I'd go ahead and log an issue with TypeScript or ask a question on StackOverflow. Maybe there's something I'm overlooking. |
@blakeembrey Hah, okay. I'll do that in a bit here. Thanks for taking a look! |
You can get part of the way with intersections or extending the base class with static methods. For example: var keyboardjs: ImportedKeyboard & {
Locale: typeof ImportedLocale;
KeyCombo: typeof ImportedKeyCombo;
Keyboard: typeof ImportedKeyboard;
KeyEvent: typeof ImportedKeyEvent;
};
export = keyboardjs; However, this only allows you to use the properties in value positions and not type positions so the eventhandler still won't work. new keyboardjs.KeyCombo('te')
class ExtendedKeyboard extends keyboardjs.Keyboard {
keydownOnce(keys: string | string[], callback: /* keyboardjs.EventHandler */): void {}
} |
Hmm, good to know. That seems really weird to me ( EDIT: Nevermind... I see why that obviously doesn't work. |
Here's the question on Stack Overflow. |
This actually turned out to be relatively simple. Let me know if I'm overlooking something but I landed on this: import { Locale as ImportedLocale } from './lib/locale';
import { KeyCombo as ImportedKeyCombo } from './lib/key-combo';
import {
Keyboard as ImportedKeyboard,
KeyEvent as ImportedKeyEvent,
EventHandler as ImportedEventHandler
} from './lib/keyboard';
export class Locale extends ImportedLocale {}
export class KeyCombo extends ImportedKeyCombo {}
export class Keyboard extends ImportedKeyboard {}
export interface KeyEvent extends ImportedKeyEvent {}
export interface EventHandler extends ImportedEventHandler {}
declare const out: Keyboard;
export default out; |
@OlsonDev You can't use |
Did you try using the the code wrote? I'm sure you'd see this issue very quickly 😄 |
Not seeing where... :-\ The docs or in another issue we've discussed?
Bummer. I don't understand how though -- I'm not familiar with the intricacies of ES6
Yes, actually. It works. No build errors from
So I committed it, updated the typings in my project that depends on this, changed a few syntax things there ( Which error were you expecting me to see...? Any specific config settings to get it to show up? |
You will have a runtime error, not a type error. |
Also, your tests in the directory don't actually test anything from what I see? |
Basically, |
I tested it at runtime too -- no error.
Heh, yup. That's a TODO, as well as putting JSDoc/whatever comments in the |
@OlsonDev That's not possible, I know that. I suppose you're using SystemJS then, which is the only platform doing that interop for you. Any other system will fail. |
Maybe |
Also, I realised you can do this: import { Locale } from './lib/locale';
import { KeyCombo } from './lib/key-combo';
import { Keyboard, KeyEvent, EventHandler } from './lib/keyboard';
declare class KeyboardJS extends Keyboard {
Locale: typeof Locale;
KeyCombo: typeof KeyCombo;
Keyboard: typeof Keyboard;
KeyEvent: typeof KeyEvent;
EventHandler: EventHandler;
}
declare var keyboardjs: KeyboardJS;
export = keyboardjs; ^ That one is technically correct. |
We could also run tests in an actual browser (E.g. PhantomJS) if you're interested in using Browserify + |
My bad -- I meant I tested the I'll try I'll try that code you just included. I assume you also meant |
@OlsonDev Possibly, yes. I did changes all over the place, sorry! Turns out one of them was making |
In the test case, what if you just fake Edit: Or whichever the library needs? |
Sure, I could make a simple mock, too. I'm also unclear as to how extensive/what the tests should be. I don't think it's the typings repo's job to test the entire library...? Just that the API surface is consistent with the claimed surface, yeah? As in, return values/outputs/whatever don't matter so much as properties/methods existing. |
@OlsonDev Yes, doesn't need to be extensive. Let's just make sure the import is tested by using a couple of the methods/classes 😉 |
Got it. 👍 Your code above actually wouldn't work. Two issues:
|
@OlsonDev THERE IS NO DEFAULT. That's what I'm saying. |
And 2 works for me. |
I'll just do a PR instead. |
@OlsonDev Explained in the PR, please don't introduce |
Okay, I'm pretty sure I get the whole "
Because you used |
In my other project, I did this: import keyboard, { Keyboard, EventHandler, KeyEvent } from 'keyboardjs/index';
class ExtendedKeyboard extends Keyboard {
keydownOnce(keys: string | string[], callback: typeof EventHandler) {
// stuff
}
}
const extended = keyboard as any as ExtendedKeyboard;
extended.keydownOnce = ExtendedKeyboard.prototype.keydownOnce;
// stuff And |
The way I've written these up for now works in an external project with
typings install --save github:OlsonDev/typed-keyboardjs#commitish
. I can do something like:However, I can't do something like:
I've looked at the examples and played around with a few different syntaxes to see what
typings
would generate with theyo typings
generated commandnpm run watch
and I'm just not experienced enough to figure this out.@blakeembrey Could you please take a look at what I have and advise specifically what I need to change? One of my biggest issues getting this far was exposing both the types and the
Keyboard
instance available viadefault
; see how KeyboardJS does its exports.The text was updated successfully, but these errors were encountered: