Error when assigning a property of type string
The compiler issues an error, but the browser works -
import * as angular from 'angular';
import * as AngularUIRouter from 'angular-ui-router';
//angular-router.ts(7,7): error TS2322: Type 'typeof AngularUIRouter' is not assignable to type 'string'.
const ANGULAR_UI_ROUTER: string = (AngularUIRouter.default);
// in browser
// string, ui.router, ui.router
console.log(typeof AngularUIRouter.default, ANGULAR_UI_ROUTER, ANGULAR_UI_ROUTER.toString());
const AngularRouter = angular.module('AngularRouter', ['ui.router']);
AngularRouter.config(($stateProvider, $urlRouterProvider)=>{
$urlRouterProvider.otherwise('/');
$stateProvider
.state('/', {
url: '/',
controller: null
})
.state('editor', {
url: '/editor',
controller: null
});
});
export default AngularRouter;
And there is no error -
import * as angular from 'angular';
import * as AngularUIRouter from 'angular-ui-router';
const ANGULAR_UI_ROUTER: string = (AngularUIRouter.default).toString();
// string, ui.router, ui.router
console.log(typeof AngularUIRouter.default, ANGULAR_UI_ROUTER, ANGULAR_UI_ROUTER.toString());
const AngularRouter = angular.module('AngularRouter', ['ui.router']);
AngularRouter.config(($stateProvider, $urlRouterProvider)=>{
$urlRouterProvider.otherwise('/');
$stateProvider
.state('/', {
url: '/',
controller: null
})
.state('editor', {
url: '/editor',
controller: null
});
});
export default AngularRouter;
This is a feature or a bug?
Can you point me to the typings file for angular-ui-router ? the one on definietlly typeddoes not seem to export a default export: https://github.com/borisyankov/DefinitelyTyped/blob/master/angular-ui-router/angular-ui-router.d.ts
I rewrote a bit for themselves....
declare module 'angular-ui-router' {
export {AngularUIRouter as default};
}
declare namespace AngularUIRouter {
export class AngularUIRouter {
}
interface IState {
name?: string;
then AngularUIRouter is really not a string. console.log works as it evaluates the object before writing it. this is not true for the general case. so if you have a function that declares it takes an argument of type string, and you pass it a class it will fail at run time.. e.g:
function hasBlah(s: string) {
return s.indexOf("blah") >= 0;
}
hasBlah(ANGULAR_UI_ROUTER); // runtime error indexOf is not a function
The typescript compiler will show you design-time errors when an assignment is unsafe. If you for whatever reason do not thing this is true (e.g. assigning a JSON object from XHR body to a typed object), you can use casts:
var obj: MyType = <MyType> parseXHRMessage(); // OK