Is there any downside to using .tsx instead of .ts all the times in typescript?

后端 未结 5 942
长情又很酷
长情又很酷 2020-12-22 16:54

I just start working on a React project with TypeScript and ask myself what should I do with regular class files? Should I use .ts or .tsx files an

5条回答
  •  悲哀的现实
    2020-12-22 17:33

    .ts files have an type assertion syntax which conflicts with the JSX grammar. In order to avoid breaking a ton of people, we use .tsx for JSX, and added the foo as Bar syntax which is allowed in both .ts and .tsx files.

    let someValue: any = "this is a string";
    let strLength: number = (someValue).length;
    

    And the other is the as-syntax:

    let someValue: any = "this is a string";
    let strLength: number = (someValue as string).length;
    

    We can use .ts with as-syntax but someValue is cool!

提交回复
热议问题