Skip to content
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

Webpack + Angular2 require in template #8514

Closed
galsch opened this issue May 7, 2016 · 10 comments
Closed

Webpack + Angular2 require in template #8514

galsch opened this issue May 7, 2016 · 10 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@galsch
Copy link

galsch commented May 7, 2016

Hi,

I'm using Webpack + Angular2 with the raw-loader. In my app I'm using something like:

@component({
selector: 'test',
template: require('./test.component.html')
})

However, I get the following error:

Error:(22, 12) TS2345: Argument of type '{ selector: string; template: {}; directives: Type[][]; }' is not assignable to parameter of type '{ selector?: string; inputs?: string[]; outputs?: string[]; properties?: string[]; events?: strin...'.
Types of property 'template' are incompatible.
Type '{}' is not assignable to type 'string'.

I fix it by using:
@component({
selector: 'test',
template: "" + require('./test.component.html') + ""
})

How can this issue be fixed?

Thanks

@aluanhaddad
Copy link
Contributor

aluanhaddad commented May 8, 2016

@galsch Create a file named ./test.component.html.ts containing solely

export default "";

Edit: this is for an environment where there is no require function

@galsch
Copy link
Author

galsch commented May 8, 2016

@aluanhaddad I created a new file with the line you gave, without doing any other changes - and I still get the same error.

Also, I want to have a solution that won't make me add a counterpart .ts file for every .html files.

Looking at https://github.com/AngularClass/angular2-webpack-starter - Angular2 + Webpack started Kit they were able to achieve it without any TypeScript errors.

Take a look at https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/home/home.component.ts

I'm not sure how they were able to achieve this without TypeScript errors.

@aluanhaddad
Copy link
Contributor

If you clone the repo and run npm install it should work correctly. Note that template is being given type any due to the definition of NodeRequire in 'typings/browser/ambient/node'. You actually do not need to create the additional file I mentioned because the provided require function will always pass the typechecker if given a string.

@galsch
Copy link
Author

galsch commented May 8, 2016

@aluanhaddad I have created a much simpler Webpack + Angular2 project which don't have all the configurations from https://github.com/AngularClass/angular2-webpack-starter and I want to take only the necessary minimum out of that repo.

Currently in my project I get the TypeScript error.

I've changed my typings.json file with

"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#8cf8164641be73e8f1e652c2a5b967c7210b6729"

I ran typings install - but I still get the same TypeScript error.

Are you sure this what fixed the TypeScript error?

@aluanhaddad
Copy link
Contributor

What compiler version and compiler settings are you using? Can you publish your repository?

@galsch
Copy link
Author

galsch commented May 8, 2016

@aluanhaddad You rock - thanks! After adding the node into typings.json and making some cleaning in my project - it finally worked.

Thanks - issue can now be closed.

@galsch galsch closed this as completed May 8, 2016
@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 9, 2016
@RyanCavanaugh
Copy link
Member

Thanks for helping out @aluanhaddad !

@aluanhaddad
Copy link
Contributor

Gladly.

@aluanhaddad
Copy link
Contributor

@okian the cast is irrelevant.

@aluanhaddad
Copy link
Contributor

With TS 2.0+ you can now write

declare module '*.html' {
    export = '';
}

In a top level declaration file. and use the import statement.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants