Try pasting that line into the TypeScript playground. If you mouse over
litValInlineNoInterfaceOrTypeMissingProp2 there, you’ll see that it has an
any type. So this is solving the error by turning off type checking, which we want to avoid.
If you want an any type, the simpler way to get it is:
let val: any = literal;
Charles Maynard you can use an identity function to be more explicit about the intended type of your literal, but I’m not sure why you’d prefer that to just declaring the type:
let obj: type = literal;
The issue you two are flagging only exists in
.tsx files (TypeScript + React JSX). It’s a syntactic issue, not a fundamental one, and is easy to work around.
An interesting and provocative post.
You’ve also switched from an arrow function to a function statement here. The fairer comparison is to:
const identity = <T>(arg: T) => arg;
It’s still “noisier,” but only by a few characters vs. the untyped version.