Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 15:50
    typescript-bot synchronize #37697
  • 15:18
    sandersn labeled #37835
  • 15:18
    sandersn labeled #37835
  • 15:18
    sandersn synchronize #15575
  • 15:18
    sandersn synchronize #28460
  • 15:18
    sandersn synchronize #31641
  • 15:17

    sandersn on master

    Fix easy misunderstanding "! ==… (compare)

  • 15:17
    sandersn closed #37838
  • 15:17
    sandersn closed #37837
  • 15:17
    sandersn assigned #37838
  • 15:16
    sandersn milestoned #37837
  • 15:16
    sandersn labeled #37837
  • 15:16
    sandersn labeled #37838
  • 10:48
    zhangciwu edited #37838
  • 08:36
    acutmore edited #37829
  • 08:00
    zhangciwu edited #37837
  • 07:50
    zhangciwu opened #37838
  • 07:47
    zhangciwu opened #37837
  • 04:31
    typescript-bot synchronize #37697
  • 04:10
    Kingwl synchronize #37727
jvquarck
@jvquarck
Hello. I upgraded Typescript from version 3.2.4 to version 3.7.5. I am facing a little problem, the command is executed from the root of the project into a workspace "${somePath}/node_modules/.bin/tsc" -p ${somePath}/tsconfig.json. The problem is that now, all the path (somePath) is generated in the folder structure of the transpilation "dist/some/path/...etc". Any idea if there is some change in tsconfig I should take into account in this new version?
jvquarck
@jvquarck
Ok, fixed. Seems the default behavior of "rootDir" changed. Setting "rootDir": ".", in the target tsconfig.json fixed my issue.
Gintautas Miselis
@Naktibalda
if properties are only set in constructor, is it better to use readonly properties or getter methods and private properties with _ prefix?
waffln
@waffln
What's the type for data that can be stringable?
For example i want to allow the types String, Number, Float, Boolean... but not Objects (at least not those that don't implement .toString())
I'm writing a sort-of polyfill for new URLSearchParams().
Paarth
@paarthenon
I don't believe there's a general type for Serializable, but you could construct a union type from those acceptable elements
waffln
@waffln
Alright, thanks
Meai1
@Meai1
how are all these .d.ts files actually generated in lib/ ? I know it says that the 'gulp LKG' generates them but I cant quite decipher how it is doing that. Shouldnt they be generated from webIDL files somewhere? I'm not seeing this
Meai1
@Meai1
as you can see, somebody seemingly writes these by hand? I dont understand the point of this, what is being generated then if it's all written by hand anyway: microsoft/TypeScript@fb403da
Bruce Pascoe
@fatcerberus
@waffln If all you care about is whether it has a toString() or not: { toString(): string }
TS is structurally typed
Rory Smith
@alanbuchanan

hi all, I am trying to be strict about the data I receive from an endpoint. I have the following function:

export async function http<T>(request: string): Promise<T> {
    const response = await axios.get(`${getBaseUrl()}${request}`)
    const body = response.data

    return body
}

and I am calling it like this:

const test = await http<Product>(
        '/get-product'
    )

where Product is:

interface Product {
    foo: string
    bar: string
    baz: string
}

The request works just fine. I get the data I am expecting.

The problem is: none of foo, bar, or baz exist on the object I get back from the resource.

So I want TS to tell me the Product I passed in is invalid!

Anyone know what I'm doing wrong?

webstrand
@webstrand
TS can't do runtime type checking
Bruce Pascoe
@fatcerberus
TS can only check types at compile time. If you're getting a bad type at runtime, you have to deal with that yourself
waffln
@waffln
@fatcerberus No, that's not what i care about, i'm looking for anything that doesn't return something like [object Object] when stringified. I mostly care about primitives.
Bruce Pascoe
@fatcerberus
Then you have to make a union of the primitive types you allow manually
waffln
@waffln
So yeah, .toString() is one part, but a type for primitives would also work, since that's what you'd put into URL params most of the time.
Alright, thanks.
This message was deleted
Bruce Pascoe
@fatcerberus
@alanbuchanan Didn't you just say you're getting the data you expect? But then you say props are missing...
Rory Smith
@alanbuchanan
@fatcerberus I forcefully made the Product interface wrong, expecting it to tell me I'm wrong. The props on the object I get back are different
Bruce Pascoe
@fatcerberus
Okay then yeah, you only get type errors for things the compiler knows about. If the type doesn't match at runtime, TS doesn't know that
This is why APIs where you have to give type arguments to a generic manually are usually a bad idea
You should be treating the data returned by the request as unknown and verify at runtime
Rory Smith
@alanbuchanan
@fatcerberus understood - I was following this article - https://www.carlrippon.com/fetch-with-async-await-and-typescript/ - would you say it is a bad idea to go down this road then?
Bruce Pascoe
@fatcerberus
In general you should never write a generic function where the type param is only used in the return type - you can't reflect on T and there's no way for the compiler to infer it
Raziel
@rraziel
Generic just for the return type is mostly a I-don't-wanna-cast-myself hint
Bruce Pascoe
@fatcerberus
So yeah that article is giving pretty bad advice for API design
Keith Layne
@keithlayne
@alanbuchanan That's just one more bad blog post.
Rory Smith
@alanbuchanan
interesting
Keith Layne
@keithlayne
amazing that it's not on medium
Raziel
@rraziel
To be fair there's not much you can do with functions like fetch
Keith Layne
@keithlayne
fetch itself is fine
Raziel
@rraziel
Except maybe pass a constructor/factory function to parse the string
Keith Layne
@keithlayne
usually boils down to JSON.parse returning any.
If it returned unknown or an easier-to-work-with Json type that would be much easier.
But all this is what validation libs are for.
Bruce Pascoe
@fatcerberus
Long story short: the type system doesn’t help you with validation. Only compile-time checks
Keith Layne
@keithlayne
another short story:
Once upon a time, there were blogs about programming. Mostly they sucked. The End.
Keith Layne
@keithlayne
I feel like the distribution is far more skewed in the domain of programming blogs. But I have no real evidence to back that up.
Raziel
@rraziel
Had a CTO few years back, I told him it could be interesting to have a developer portal so increase visibility, help, etc (I guess I don't need to explain why a developer portal can be a good thing)
He replied "people who have time to write blogs and stuff are people who are not good enough to have a job"
(he had many other issues, communication was clearly one of them :D)
But yeah, it's difficult to find good information outside official sites/doc
Keith Layne
@keithlayne
and tests are worthless
that's busy work for interns