Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:00
    typescript-bot closed #35092
  • 10:00
    typescript-bot closed #35606
  • 08:54
    mantissa7 closed #35645
  • 04:45
    MicahZoltu closed #35657
  • 04:23
    typescript-bot synchronize #35665
  • 03:52
    sheetalkamat synchronize #15575
  • 03:52
    sheetalkamat synchronize #28460
  • 03:52
    sheetalkamat synchronize #31641
  • 03:51

    sheetalkamat on noEmitOnError

    (compare)

  • 03:51

    sheetalkamat on master

    Do not write files directly f… (compare)

  • 03:51
    sheetalkamat closed #34823
  • 03:51
    sheetalkamat closed #34832
  • 02:35
    trusktr edited #31527
  • 01:59
    sandersn review_requested #35661
  • 01:59
    sandersn review_requested #35661
  • 01:59
    sandersn edited #35661
  • 01:58
    sandersn synchronize #35661
  • 01:58

    sandersn on remove-object-rewrite

    flip boolean right way round (compare)

  • 01:58
    sandersn synchronize #35661
  • 01:58

    sandersn on remove-object-rewrite

    reinstate object->any rewrite w… (compare)

anthalon
@anthalon

I have an object that accepts a string parameter in it's constructor, and it's toString() formats that string in a specific way.

for instance:

export class Currency
{
    constructor(private amt: string) {}
    ...
    toString() { /* return formatted string here, comma separated etc */ }
}

amd if I use an object of this class directly, it returns the formatted string.

But when I try to parse from json, the Currency object is treated as string, and doesn't format with it's toString() method.

How can I initialize this object from json so that it's treated as a Currency object?
Keith Layne
@keithlayne
you probably know this, but that has nothing to do with TS. You're going to have to write some kind of deserialization function.
anthalon
@anthalon
the Currency class is part of a model, and it's being given a string value when the json is JSON.parsed. Instead of doing this manually, I was wondering if typescript offers some functionality to initialize these objects, maybe in a way where the string is not assigned to the object, but instead passed in as a constructor parameter?
Bruce Pascoe
@fatcerberus
All the functionality typescript adds is compile-time only, it doesn’t help you at runtime
What I think you want is a “reviver”, google “json reviver”
anthalon
@anthalon
yup, that's what I'm after. Thanks
Thanzex
@Thanzex
What are the best practices to export constants from a package?
Anthony Lukach
@alukach

Does TS not allow typing a function without directly calling it?

e.g.

[{x: 1}].map(Object.entries<number>) // 'number' only refers to a type, but is being used as a value here.
Bruce Pascoe
@fatcerberus
Yeah, you can’t instantiate the generic without calling the function, we were discussing that here recently
Keith Layne
@keithlayne
if you could....
Bruce Pascoe
@fatcerberus
It actually seems like it’s interpreting the angle brackets as comparison operators there, which is odd
Anthony Lukach
@alukach
Is it clear what I'm trying to do? This would be helpful for composing "point free" functions (I'm using ramda). WIthout adding a type to Object.entries, TS thinkings the 1 index of the tuple is any rather than number... Is there a way to achieve my goal?
Keith Layne
@keithlayne
yeah, we all understand what you want
Anthony Lukach
@alukach
Keith Layne
@keithlayne
write a lambda where you specify the type
Anthony Lukach
@alukach
well that's not much fun, but if that's the way it has to be...
Keith Layne
@keithlayne
for your own functions you can sort of work around by writing a generic interface with a call signature.
Bruce Pascoe
@fatcerberus
The compiler sees a syntax error in reality but it always tries to recover and emit valid code so that makes sense
Keith Layne
@keithlayne
whoosh
Bruce Pascoe
@fatcerberus
Huh?
Anthony Lukach
@alukach
@fatcerberus :thumbsup: Oops, I spaced the error about Property 'entries' does not exist on type 'ObjectConstructor'.
Thanzex
@Thanzex

I'm trying to create a package to export some constants and interfaces that we need for a project. The interfaces work just fine, but trying to import and use one of the exported constants gives an error in compilation with webpack.
WARNING in ./src/app/app.component.ts 6:17-20 "export 'CONSTANT' was not found in 'package'
My package is made of:
constants.ts

export const TEST="test";

index.js

import { TEST } from './constants'

module.exports = { TEST }

index.d.ts

export { TEST} from './constants'

After searching for a while i found webpack/webpack#7378 that seems to dive into the problem but i haven't found a soultion yet.
Any ideas?

Keith Layne
@keithlayne
you need to export { TEST } from './constants'
Thanzex
@Thanzex
yup sorry, i re-wrote everything for the message, there are no typos
Keith Layne
@keithlayne
and in the types. Not sure what your mixed case meant. Okay.
Do you have a constants.d.ts?
and are you exporting the constant in JS somewhere?
Thanzex
@Thanzex
in the message i forgot to add the module exports in the js file
@keithlayne what do you mean?
i do not have a constants.d.ts
Keith Layne
@keithlayne
well you're referencing it.
Thanzex
@Thanzex
i do have many more files that contain only interfaces without the *.d.ts file and they work fine with this setup
Keith Layne
@keithlayne
are you writing in mixed js/ts?
Thanzex
@Thanzex
nope, only ts, but the package started as a normal js one
Keith Layne
@keithlayne
if you have tsc emit your declarations you shouldn't have a problem I think.
Unless it's something weird that I don't understand, which is totally possible.
what I was getting at is that tsc will generate a .d.ts per .ts file, and they'll reference each other just like the JS files.
I'm still not clear if you're writing those declarations by hand or not.
Thanzex
@Thanzex
in the issue that i referenced i foud out about isolatedModules. Apparently if you declare a constant in a file and export it, export it fro another file again like export { foo } from './bar' it's like the declaration doesn't exist
it was all done by hand
Keith Layne
@keithlayne
yeah you shouldn't do that, especially when tsc can do it for you. Why isolatedModules? Using babel or something?
sorry, got a meeting for now
Thanzex
@Thanzex
yup, using webpack
sorry, forgot to add
Keith Layne
@keithlayne
yeah I never really use any bundlers on npms. I can imagine some use cases, but I generally let the consumer decide if/how to bundle.
Bruce Pascoe
@fatcerberus
You can't do export from with isolatedModules afaik
With that setting on the compiler is basically looking at each file individually so it's not able to thread the types through
Since you're using webpack I recommend disabling isolatedModules and using ts-loader
Then webpack will compile the TS directly as part of the build