-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New user experience: type/variable confusion #14450
Comments
This was a matter of confusion in the end. type Button = ({ title: string }) => JSX.Element
const Button: Button = ({ title }) => <button>{title}</button>
<Button title={42} /> The above doesn't error out, though I imagined it should. The correct type Button = (props: { title: string }) => JSX.Element Any chance we can get some kind of error/notice on the previous |
type add2 = (number) => (number) => number is equivalent to type add2 = (number: any) => (number: any) => number Note that setting |
@gcanti Thanks! The errors that option supplies makes it much easier to remember about accidental type shadowing. I do wonder if there's a world in-between where the default warns when you shadow type names by variable name—fewer gotchas for newcomers. |
I've made some edits above to clarify where I believe a confusing situation for new users could be made more friendly. |
tslint rule from tslint-microsoft-contrib has already supported this feature.
type add2 = (number) => (number) => number // warning
const add2: add2 = x => y => x + y
add2("no")("no")
It will be a good experience for new user, although I think it is the role of linter. |
@RyanCavanaugh Fair enough, though doesn't the fact that this keeps coming up make you consider it an open user experience issue? |
|
What I mean is, if you have a concrete suggestion, we're definitely willing to hear it, but we've thought about this a lot and haven't come up with a satisfactory solution, and thus consider this "closed" in the sense of "There are no good options on the table despite substantial effort, and we don't think more effort will produce new options". |
My concrete suggestion from the OP:
|
(If I'm not being clear enough, this would be a new configuration option that lives alongside |
That'd be a breaking change similar to turning on |
Could you elaborate? A new compiler warning with helpful instructions on how to disable seems like a fair tradeoff. I'm happy to drop this, it's just definitely a pain point for new user on-boarding, and I've been looking to the helpful experiences that Elm and Swift (sometimes) provide. I imagine there'd be an interest in making it as painless as possible to introduce new users of all experiences to TypeScript. |
There is definitely code in the real world which uses name-only function signatures on purpose. Our general rule is that we only introduce new warnings when we strongly believe those warnings are "correct" (i.e. we found a bug where we failed to find a bug before). Anything else is behind an opt-in flag. So since you'd have to opt in to this flag anyway, the question is, who turns on this flag but doesn't want to turn on |
TypeScript Version: 2.1.1
Variable/type name confusion makes debugging difficult with default configuration.
Code
Leads to the expected error:
My experience with other type systems led me to try this when abstracting the function type signature into a
type
alias:This, I would find out, is because the above is
type
is actually the following signature:Expected behavior:
This was a bit confusing, coming from other languages where variable names are usually meaningless in type signatures. I'd hope for a gentle warning with a configuration option to disable. This might make the experience a bit more forgiving to folks new to TypeScript.
It turns out that setting
"noImplicitAny"
totrue
easily flags these situations, but that option is opt-in.Actual behavior:
No warning.
The text was updated successfully, but these errors were encountered: