Skip to content
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

Adding a tsconfig option to disallow implicit never and unknown #57406

Closed
6 tasks done
Klopfsalat opened this issue Feb 14, 2024 · 6 comments
Closed
6 tasks done

Adding a tsconfig option to disallow implicit never and unknown #57406

Klopfsalat opened this issue Feb 14, 2024 · 6 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@Klopfsalat
Copy link

πŸ” Search Terms

"never", "unknown", "tsconfig", "implicit"

βœ… Viability Checklist

⭐ Suggestion

never and unknown unlike any dont have a tsconfig.json option.
My suggestion is to add an option to the file similar to any.
To avoid problems i would suggest to set it as false and to later on add it with true to the strict mode in later versions to give people time to adjust.
tsconfig options could look like the following:

{
    "compilerOptions": {
        ...
        "noImplicitUnkown": false,
        "noImplicitNever": false,
        ...
    },
    ...
}

πŸ“ƒ Motivating Example

It would lead to cleaner coding and less problems when strict mode is on.

πŸ’» Use Cases

Q: What do you want to use this for?
A: To easily get the compiler to show me when there are variables that have unintended never/unknown as datatype.

Q. What shortcomings exist with current approaches?
A: People create variables that get read as never/unknown by the compiler and when others try to clean it up later they have a harder time understanding what the intended value should be. It leads to worse code.

Q: What workarounds are you using in the meantime?
A: Using Eslint.

@fatcerberus
Copy link

I’m confused β€” there isn’t a such thing as an β€œimplicit” unknown or never in the same sense that noImplicitAny is meant to guard against. Can you give an example of some code that you think these flags should error on? (that’s what the Motivating Example section is for, btw)

@RyanCavanaugh
Copy link
Member

I don't know what an "implicit unknown" or "implicit never" are either. Code examples when describing suggestions for a programming language are very helpful.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Feb 14, 2024
@Klopfsalat
Copy link
Author

with never it's a problem that probably only occurs when you try to change a project that wasn't on strict to a strict project. But i saw several occurrences of it and found it weird that its not getting marked like implicit any:

import { useState } from "react"
const [items , setItems] = useState([]);

Btw this obviously not pretty but doesn't create error telling me that there is an array created of type never. I would like this to get only approved by the compiler when i for some reason type the following:

import { useState } from "react"
const [items , setItems] = useState<never[]>([]);

@Klopfsalat
Copy link
Author

Klopfsalat commented Feb 14, 2024

i was wrong with unknown though, i dont think there is an example, overestimated the way noImplicitAny guards against any

@RyanCavanaugh
Copy link
Member

I'm not understanding the motivation here.

If you create a never[], as shown here:

  • If you read from it, it's correctly never (or never | undefined under NUIA)
  • You can't write to it, so any mistakes are caught

Whereas with noImplicitAny, any[] is very dangerous:

  • All reads produce any, which is dangerous
  • All writes are allowed, which is arguably dangerous

When people wanted noImplicitAny there were many examples of coding errors that wouldn't otherwise get caught without it. What are the corresponding examples here?

@rotu
Copy link

rotu commented Feb 20, 2024

The never[] issue had been brought up recently (#57419 for background).

If you create a never[], as shown here:

  • If you read from it, it's correctly never (or never | undefined under NUIA)
  • You can't write to it, so any mistakes are caught

I still don't think it's safe:

const obj = { a: [] }
const pushA = <T,>(blah: {a: T[]}, a:T)=>{
    blah.a.push(a)
}
pushA(obj, 4)
pushA(obj, "foo")
// obj.a is now an array containing [4, 'foo'] but typed as never[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

4 participants