Materialize: Global declaration 'M' not supported by Typescript

Created on 24 Nov 2017  路  10Comments  路  Source: Dogfalo/materialize

Typescript doesn't seem to support global declarations of variables with a single capital letter.

Reproduction

Declare the materialize variable in your typescript file as follows

declare const M: any;

Expected result

Linters and editors see declaration as valid.

Actual result

Linters and editors see declaration as invalid.

Linter error

Cannot find namespace 'M'.

Imports compared on Atom Editor using Typescript Language

screenshot from 2017-11-24 10-04-22
_As you can see from the screenshot, a single letter declaration seems to be unsupported._

Other information

  • Using Typescript v2.5.3
  • Maybe it would be a better idea to use more than one letter such as Mat, Materialize etc.

Most helpful comment

You can also use the Materialize Typings npm package:

npm install --save @types/materialize-css

All 10 comments

We don't use typescript.

No interest in supporting it?

Most Angular projects will be using typescript.

Is this something that we fix in the library or something that can only be fixed in the client code?

@Dogfalo I suspect the way to fix this would be to use any global naming scheme that isn't a single capital letter (such as Mat, Materialize etc.).

This would also reduce the likelihood of conflicts with other packages.

I have successfully added typescript support for M.

typings/materialize-css/index.d.ts (not completed):
````ts
export as namespace M;

export class Sidenav{
public constructor(elem: Element, options?: SidenavOptions);

public static getInstance(elem: Element): Sidenav;
public open(): void;
public close(): void;
public destroy(): void;

}

export interface SidenavOptions {
edge: string
}
````

Then add the following things to your tsconfig.json:
json { "compilerOptions": { "baseUrl": ".", "paths": { "*": ["typings/*", "*"] }, "typeRoots": [ "./node_modules/@types", "./typings" ] } }

Now you should able to use M in any way you like. e.g. as a global variable, or import * as M from 'materialize-css'

I believe @huww98 has the correct solution for integrating Materialize with Typescript.

You can also use the Materialize Typings npm package:

npm install --save @types/materialize-css

Include "types": ["materialize-css"] in your tsconfig.app.json file

Include "types": ["materialize-css"] in your tsconfig.app.json file

I Have add for u solution but not work variable line export = M in @types/materialize-css/index.d.ts
What can i do? TypeScript not see this variable M but how? I have use dropdown but no know to use or add my app.component for now working. Did u know to make?
I install @types/materialize-css and materialize-css but dropdown not working. I add materialize.min.js and css to header and again not work this script. Very hard problem starting on this framework. How?

You need to install @types/materialize-css and then

import * from "materialize-css";

M.AutoInit();

works.....
I also included "types": ["materialize-css"] in my tsconfig.app.json file as suggested by @deser1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samybob1 picture samybob1  路  3Comments

alexknipfer picture alexknipfer  路  3Comments

PhillippOhlandt picture PhillippOhlandt  路  3Comments

djensen47 picture djensen47  路  3Comments

Robouste picture Robouste  路  3Comments