imaskjs icon indicating copy to clipboard operation
imaskjs copied to clipboard

IMaskInput type not working properly "Typescript"

Open pilucdes opened this issue 5 years ago • 17 comments

Describe the bug IMaskInput is not typed properly and result with this typing -> const IMaskInput: React.ComponentType<{}> IMaskInput is not taking any props like mask,definitions etc. and make it not usable with typescript at all (Atleast the way i'm configured). image

Is there something I'm missing ?

To Reproduce I have typescript 4.4.3, React 17.0.2 with Imask 6.2.2

Environment:

  • OS: Windows
  • Browser Chrome
  • IMask 6.2.2
  • typescript 4.4.3, React 17.0.2

pilucdes avatar Sep 24 '21 12:09 pilucdes

I am experiencing the same issue

momasz avatar Sep 24 '21 17:09 momasz

I'm getting the same error and don't know how to handle it yet.

hoangtrung99 avatar Sep 24 '21 18:09 hoangtrung99

I am also having the same issue, will update if I can come up with a fix other than just using //@ts-ignore on each line.

dylancrockett avatar Sep 25 '21 00:09 dylancrockett

Pretty certain it's because the return type on IMaskMixin overwrites the incoming types. So it goes through all the effort of bringing in a typed component, just to return a blank React.ComponentType.

https://github.com/uNmAnNeR/imaskjs/blob/2c5dad60d62d21614b895138c6c0369219ab88a4/packages/react-imask/src/mixin.ts#L108-L114

On compile, I think typescript notices this and simplifies the compiled types for anything wrapped with IMaskMixin to the following output

import React from 'react';
declare const IMaskInput: React.ComponentType<{}>;
export default IMaskInput;

alb7h avatar Sep 28 '21 09:09 alb7h

Downgrading to 6.1.0 solves while it is not solved.

lgberro avatar Sep 29 '21 15:09 lgberro

Having this issue as well. Downgrading did not work for me because types are not available in 6.1.0. We are also not allowed to use ts-ignore, therefore adding //@ts-expect-error with a description solves the issue for now. Looking forward to an update / fix.

benkburke avatar Nov 12 '21 15:11 benkburke

Any updates on when this will be fixed?

ReschAndr avatar Dec 01 '21 10:12 ReschAndr

Any suggestion ?

mcatal avatar Dec 10 '21 06:12 mcatal

@mcatal

I add a this temp fix to pass the build:

// @ts-ignore: hot fix for https://github.com/uNmAnNeR/imaskjs/issues/554
import { IMaskInput } from 'react-imask';

error:

Step #0: Type error: Could not find a declaration file for module 'react-imask'. '/app/node_modules/react-imask/dist/react-imask.js' implicitly has an 'any' type.
Step #0:   Try `npm i --save-dev @types/react-imask` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-imask';`
Step #0: 

romelgomez avatar Dec 13 '21 16:12 romelgomez

To work properly with typescript I found that the path to index.d.ts should be added in types property in package.json file. I guess it could fix issues with typescript. Now it is missed here: https://github.com/uNmAnNeR/imaskjs/blob/master/packages/react-imask/package.json

Documentation: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html

AndrewGulidov avatar Dec 23 '21 10:12 AndrewGulidov

There exist a pull request that can close this issue -> https://github.com/uNmAnNeR/imaskjs/pull/576

gynekolog avatar Dec 23 '21 11:12 gynekolog

While that PR is in the wings, there is a change you can make to typeRoots in your tsconfig.json file to tell Typescript where these type files are within react-imask. https://www.typescriptlang.org/tsconfig#typeRoots

"compilerOptions": {
  ...,
  // If you don't already use this config option, you'll need to explicitly add your node_module/@types folders
 "typeRoots": [
   "./node_modules/@types",
   "../../node_modules/@types", // If you're in a workspace, you may have a hoisted @types.
   /**
    *  react-imask has types but does not specify where they are in its package.json file.
    *  @see https://github.com/uNmAnNeR/imaskjs/pull/576
    */
   "../../node_modules/react-imask/dist/index.d.ts",
 ],
}

JonKrone avatar Jan 04 '22 05:01 JonKrone

Since none of the provided solutions here worked for me, my quick and dirty workaround is to change the types of the component to any

For using IMaskMixing, (React.ComponentType in the last line in mixing.d.ts): export default function IMaskMixin<Opts extends IMask.AnyMaskedOptions = IMask.AnyMaskedOptions, Unmask extends ('typed' | boolean) = false, Value = Unmask extends 'typed' ? IMask.InputMask<Opts>['typedValue'] : Unmask extends Falsy ? IMask.InputMask<Opts>['value'] : IMask.InputMask<Opts>['unmaskedValue']>(ComposedComponent: React.ComponentType<IMaskMixinProps<Opts, Unmask, Value>>): React.ComponentType<any>;

For using IMaskInput replace in input.d.ts): import React from 'react'; declare const IMaskInput: React.ComponentType<any>; export default IMaskInput;

xzessmedia avatar Jan 19 '22 10:01 xzessmedia

@uNmAnNeR It appears it's not possible to pass unmask to IMaskMixin without type errors. Is this by design or an accident?

Happy to open another issue as relevant.

image

RyKilleen avatar Apr 30 '22 16:04 RyKilleen

@uNmAnNeR It appears it's not possible to pass unmask to IMaskMixin without type errors. Is this by design or an accident?

Happy to open another issue as relevant.

image

I'm also experiencing this issue

itsclarke avatar May 05 '22 18:05 itsclarke

@uNmAnNeR It appears it's not possible to pass unmask to IMaskMixin without type errors. Is this by design or an accident? Happy to open another issue as relevant. image

I'm also experiencing this issue

Same here. Any workarounds?

kumold avatar May 20 '22 06:05 kumold

I'm using hook-form and react-imask, and it was solved using these versions:

"react-hook-form": "^7.51.0",
"react-imask": "7.1.3"

maganezf avatar Mar 13 '24 21:03 maganezf