1

Using typescript checker only with VSCode on a javascript React project with a jsconfig.json I will need disable/bypass some typescript checks like missing property on objects and omited props.

for Example:

const Comp = ({ color, center, dark }) => <div> /* real code here */ </div>


<Comp color="blue" />   // TS check complaint on missing property: ts 2739

I would like to be able to use some TS check and disable others.

I try, without luck:

// global.d.ts
declare global {
  namespace JSX {
    type Element = string;
    interface ElementAttributesProperty {
    }
  }
}
2
  • 1
    It's not possible to disable certain messages. Why do you want to do this? Is it because center and dark are optional? Or are you in a hurry and you don't want to write types? (In the latter case, perhaps you could write a .jsx file instead of a .tsx file.) Another option is to use //@ts-ignore Commented Feb 10, 2019 at 9:35
  • I just want to use only certains features of typescript on the editor and avoid write some types, specially React related. My files are .js Commented Feb 10, 2019 at 9:40

2 Answers 2

1

If you really want to avoid writing types for those properties, then you could use any:

const Comp = ({ color, center, dark }: any) => <div> /* real code here */ </div>

You will of course need to disable the no-any rule, assuming you have that enabled (you should). You can disable it for the entire file by putting this earlier in the file:

/* tslint:disable:no-any */

Of course, doing this will lose the ability for TypeScript to warn you when you are passing the wrong properties! But at least it will make it clear where the missing types are, and you can always go over later and clean up.

In our team, we discourage people from using any and instead encourage them to get used to writing types for everything. This did slow us down at first, but it didn't take long for us to get used to doing it.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you haven't specified the signature of your component's props. You could solve this by declaring an interface

interface CompProps {
  color: string;
  center: boolean; // or whatever its type is
  dark: boolean;
}

const Comp = ({ color, center, dark }: CompProps) => <div>{/* real code here */}</div>

1 Comment

I'm trying to avoid write React related types. I will like to add some ambient JSX declaration in order to avoid specified Components type declarations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.