mcc,
@mcc@mastodon.social avatar

Hey I'm used to running TypeScript with "noImplicitAny" but now I'm converting some code from js to ts and there's too many untyped variables already in. Is there a way, in non-"noImplicitAny" TS, to say "THIS VARIABLE must not be any"? Like say I'm saying let glarg = require("blarg");' and I want to demand that the type inference infer something other than any on a particular line.

lambdageek, (edited )
@lambdageek@mastodon.social avatar
mcc,
@mcc@mastodon.social avatar

@lambdageek oh this rules

jleedev,
@jleedev@mastodon.sdf.org avatar

@mcc @lambdageek Aha, ‘any extends T’ is more succinct than ‘unknown extends T’ and ‘T extends unknown’.

lambdageek,
@lambdageek@mastodon.social avatar

@jleedev @mcc I'm really surprised it worked. I'm used to any just completely destroying all typechecking in TS

simrob,
@simrob@social.wub.site avatar

@lambdageek @jleedev @mcc that's excellently cursed thank you for introducing me to this concept

lambdageek,
@lambdageek@mastodon.social avatar

@simrob @jleedev @mcc hmm...

This doesn't work right after all...

NotAny<any> and NotAny<unknown> both reduce to never.
(Checking for T extends unknown & unknown extends T doesn't work either)

Defining SameType<T,U> in the obvious way it seems like any and unknown are hard to distinguish. Although there are some differences in behavior of SameType<>, so maybe something can be cooked together. Although I'm not sure if it will be robust

mcc,
@mcc@mastodon.social avatar

@lambdageek @simrob @jleedev is there any situation where barring any is desirable but barring unknown is undesirable?

lambdageek,
@lambdageek@mastodon.social avatar

@mcc @simrob @jleedev I personally tend to use "x as unknown" to mean "I'm too lazy to give this a proper type, but I'm just sinking it into some browser API for now, but don't let me use it in anything real, later"

lambdageek,
@lambdageek@mastodon.social avatar
lambdageek,
@lambdageek@mastodon.social avatar

@simrob @jleedev @mcc I guess the "good" news is that the difference in how 'any' and 'unknown' behave in distributive conditional types is intentional and so building a type-test that exploits that should be possible and might even be somewhat robust.

But... yuck...

mcc,
@mcc@mastodon.social avatar

@lambdageek @simrob @jleedev I just renamed NotAny<T> to Known<T> and now it works as named and serves the purpose I needed it to. Thanks

mcc,
@mcc@mastodon.social avatar

@lambdageek Also it means I can now recreate the Autechre song "known(1)" in valid TypeScript so that's cool

lambdageek,
@lambdageek@mastodon.social avatar
watzon,

@mcc the most typescripty way to do this if you have noImplicitAny on would be to create an interface for the thing you're importing, and then do let glarg = require("blarg") as YourInterface. If I'm understanding your question right.

Alternatively you can use as unknown which skirts the noImplicitAny rules, but it will make you check that whatever you're trying to access on the object is there before using it.

Hope this makes sense!

mcc,
@mcc@mastodon.social avatar

@watzon @moh_kohn The problem is I don't know the type of what is being returned. I just want to assert it's not any.

(The ACTUAL problem is that I'm calling "require", and there's a @types it's supposed to be picking up, but I think it's somehow not. But I'm not even sure what type it's inferring right now.)

kainino0x,
@kainino0x@mastodon.social avatar

@mcc @watzon @moh_kohn @types TypeScript language servers (conveniently built into and on by default in VS Code) can show you the inferred type of a variable on mouseover (or equivalent gesture in your editor).

mcc,
@mcc@mastodon.social avatar

@kainino0x @watzon @moh_kohn Yes, I have one installed. It is not working.

My experience is this is pretty normal. New project, typescript typings aren't working, or import isn't working, or my LSP suddenly isn't working…

kainino0x,
@kainino0x@mastodon.social avatar

@mcc @watzon @moh_kohn :( well I won't be of any help then

mcc,
@mcc@mastodon.social avatar

@watzon I am trying to do the opposite of this. I am trying to not use noImplicitAny, and get it to selectively apply noImplicitAny on the line (because it should not be any).

The reasno I am trying to not use noImplicitAny is the file has 95 instances of implicit any and fixing, or //ts-ignoring, all 95 would be inconvenient.

mcc,
@mcc@mastodon.social avatar

Alternately, can I selectively assert noImplicitAny for individual function bodies?

lambdageek,
@lambdageek@mastodon.social avatar

@mcc Looks like there's a PR open to allow some tsconfig.json compilerOptions as pragmas within individual files, but it hasn't been merged yet (and it's been open for a year). So I'm guessing "per function" is some ways away

https://github.com/microsoft/TypeScript/pull/49886

mcc,
@mcc@mastodon.social avatar

@lambdageek Thanks. I guess at least it's nice that's on the roadmap now.

It does seem surprising there is no type for "notAny" in the type system, tho.

mcc,
@mcc@mastodon.social avatar

@lambdageek I guess they just assume everyone runs with noImplicitAny all the time.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • cubers
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • JUstTest
  • everett
  • tacticalgear
  • modclub
  • anitta
  • cisconetworking
  • tester
  • ngwrru68w68
  • GTA5RPClips
  • normalnudes
  • megavids
  • Leos
  • provamag3
  • lostlight
  • All magazines