1

I would like to create a "Utility Module": tnt.module.ts:

import {NgModule} from '@angular/core';
import {toUpperCase} from "./tnt.utils";

@NgModule({
  declarations: [
    toUpperCase
  ],
  exports: [
    toUpperCase
  ]
})
export default class TntModule {}

Here is one util function example: tnt.utils.ts

export const toUpperCase= function (str:String) {
  return str.toUpperCase()
}

I am getting an Error:

ERROR in [default] /data/2016/le-tube/src/app/shared/tnt/tnt.module.ts:5:10 
Argument of type '{ imports: undefined[]; declarations: ((str: String) => string)[]; exports: ((str: String) => str...' is not assignable to parameter of type 'NgModule'.
  Types of property 'declarations' are incompatible.
    Type '((str: String) => string)[]' is not assignable to type '(any[] | Type<any>)[]'.
      Type '(str: String) => string' is not assignable to type 'any[] | Type<any>'.
        Type '(str: String) => string' is not assignable to type 'Type<any>'.
          Type '(str: String) => string' provides no match for the signature 'new (...args: any[]): any'

What am I missing ? Why can't I create a module with simple functions ? I suppose I am just missing a simple detail but I can't figure it out...

Brett
  • 1,717
  • 1
  • 26
  • 38
  • I can't answer it from the info provided. However, I can advice you to use the angular cli: https://github.com/angular/angular-cli and toUppercase seems to me a 'pipe' candidate. For functions you should use a service. – user3791775 Sep 26 '16 at 00:01
  • I am using the angular-cli, I see thanks I will build a service instead. – Brett Sep 26 '16 at 00:58

2 Answers2

0

You can not export a function from NgModule,

exports : Array|any[]>

Specifies a list of directives/pipes/module that can be used within the template of any component that is part of an angular module that imports this angular module.

Read more about it here

Hope this helps!!

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69
0

You can not export functions from modules.

You have two options:

1) either create a utililty.ts file and create your functions in there to be exported.

export function utlityFunction1(param1, param2) { return param1+param2; )

you can import this function in your code file just like any other object.

import { utilityFunction } from './utility.ts'

2) Create a UtilityService and inject that into your component.

More on that here https://angular.io/docs/ts/latest/guide/dependency-injection.html

Alexander Ciesielski
  • 10,506
  • 5
  • 45
  • 66