Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm using visual studio code for a typescript project, where I use some 3rd party npm js libraries. Some of them don't provide any ts types (types.d.ts file), so whenever I use parameters or variables without specifying their type, vs code's linting shows this error:
parameter implicitly has an 'any' type.
Also, ts wouldn't compile.
How can I prevent this from happening?
–
First, to make typescript tolerate parameters without declaring their type, edit the
tsconfig.json
// disable this rule:
// "strict": true,
// enable this rule:
"noImplicitAny": false
Second, install the tslint npm package as a prerequisite for the tslint vs code extension
npm install -g tslint
Third, install the tslint vs code extension
–
–
–
–
–
** I wouldn't want to modify the config file and dumb down typescript !!
This code was throwing me error:
onDelete(todo) {
console.log('delete')
this.deleteTodo.emit(todo)
I fixed mine with adding an "any" type:
onDelete(todo: any) {
console.log('delete')
this.deleteTodo.emit(todo)
–
I ended up here with the following error. This is the first search result on Bing, so, putting my solution if it helps someone.
Parameter 'onPerfEntry' implicitly has an 'any' type. TS7006
I solved it like this.
Before
const reportWebVitals = onPerfEntry => {
After
const reportWebVitals = (onPerfEntry : any) => {
I understand its a simple thing but for a beginner like myself, this took a while for me to figure out.
For those who :
have this issue when importing js files like import mymodule from '@/path/to/mymodule.js'
and don't want to turn noImplicitAny false
you can:
create a file named index.d.ts
put there something like
declare module '@/path/to/mymodule.js'
// or using wild card *, like `declare module '@/store/*.js'`
For error Parameter 'dateText' implicitly has an 'any' type.
As a starting point, read the error literally, and barring other more-proper solutions, use any
like below:
TS ERROR: Plain JS
myArray.find( x => x.someValue == comparisonValue)
TS SUCCESS: Typescript
myArray.find( (x:any) => x.someValue == comparisonValue)
OP's original problem:
Error Code look like below
$(() => {
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1920:2099',
onSelect: (dateText) => {
this.dob.setValue(dateText);
Solution error code looks like below
$(() => {
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1920:2099',
onSelect: (dateText:any) => {
this.dob.setValue(dateText);
If a third party library doesn't provide types, first do an npm search for @types/SOMELIBRARY
(replace SOMELIBRARY with the npm name of the module):
npm search @types/SOMELIBRARY
If that exists, npm install it:
npm install @types/SOMELIBRARY
This is a repository of types defined for projects that don't do it themselves. If type definitions don't exist there, you can define the type yourself:
// yourmodule.d.ts
declare module "yourmodule" {
export default function somefunction(...): T
Typescript should find that and use it to declare the types in yourmodule
.
Look here for more information: https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html
You could simply define all the parts of yourmodule
as any
here, as a temporary solution, with the ability to easily add more details as you go. If you end up with a pretty nice and usable yourmodule.d.ts
, consider adding it to DefinitelyTyped: https://github.com/DefinitelyTyped/DefinitelyTyped
This is where the above @types/LIBRARY
type definitions come from.
selector: 'app-info',
templateUrl: './info.component.html',
styleUrls: ['./info.component.css']
export class InfoComponent implements OnInit {
constructor() { }
ngOnInit(): void {
onNameChange(ngModelObj){
console.log(ngModelObj);
enter code here
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.