class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

question: Keep same name as specified in `Expose({name: 'abc'})` in typescript definition

Open felixhagspiel opened this issue 2 years ago • 5 comments

I was trying to... I am using Typescript + NestJs + class-transformers for my response-DTOs. I use some of the created backend type definitions in the frontend project.

The problem: When I rename a parameter like: @Expose({ name: 'bar' }) this is not reflected in the generated typescript class.

This is an example DTO:

export class TestResponse {
  @Expose({ name: 'bar' })
  foo: string;

  constructor(data) {
    Object.assign(this, data);
  }
}

Generated typescript is:

export declare class TestResponse {
    foo: string;
    constructor(data: any);
}

Generated typescript should be:

export declare class TestResponse {
    bar: string;
    constructor(data: any);
}

I am not sure if this doable / fixable, but if anyone has a solution for this I would be very happy :)

felixhagspiel avatar May 15 '22 00:05 felixhagspiel

When I rename a parameter like: @Expose({ name: 'bar' }) this is not reflected in the generated typescript class.

this would only affect any transformations done with class-transformer at runtime, it will not affect the typescript output (it never did), you would have to rename the value itself for it to be changed in the output (like vscode Rename Symbol function)

also if you want to have it changed in pure types (the resulting .d.ts file) then you would have to manually add either a alias to the class or a new interface that defines the new property and removes the old one, like interface YourInterface extends Omit<OriginalClass, 'originalField'> { newfield: string }

hasezoey avatar Jul 31 '22 10:07 hasezoey

i think i'm encountering this same issue as @felixhagspiel.

given the following example:

export class TestResponse {
  @Expose({ name: 'bar' })
  foo!: string;
}

...i'd expect to be able to convert a plain object with the attribute foo into a TestResponse like so:

const testResponse = plainToClass(TestResponse, { foo: 'test' });

however, when i do this, trying to access testResponse.bar (the name specified in @Expose) results in a compiler error.

image

am i missing something here? @hasezoey could you provide an example that resolves this?

finalgrrrl avatar Aug 25 '22 16:08 finalgrrrl

however, when i do this, trying to access testResponse.bar (the name specified in @Expose) results in a compiler error.

that is a type error, this error is because decorators cannot affect types (yet), so you would need to manually define that property in a type


aside from that i am not familiar with class-transformer's name option, so i cannot say much but i think at least in Expose that renaming is with plain -> Class name(rename) -> decorator property name(bar -> foo) and Class -> plain decorator property name -> name(rename)(foo -> bar) and probably does not work the other way around (like plain -> Class decorator property name -> name(rename)(foo -> bar), or said differently: using the renamed and unnamed property in opposite ways) at my understanding of the name option is a rename(property with the other name does not exist, only one at a time) instead of just a alias(properties refer to the same value / have a duplicate of the same value)

^ or a simpler to read matrix of what i tried to say above:

function/way from property to property does work?
plainToClass bar foo y
classToPlain foo bar y
plainToClass foo bar n
classToPlain bar foo n
plainToClass bar bar n
classToPlain foo foo n

hasezoey avatar Aug 25 '22 17:08 hasezoey

thanks for the quick response! i think it makes sense to me why this doesn't work, seems like a tough constraint to work around with typescript (at least as class-transformer works today).

maybe we could clarify the documentation? my read of Expose as its described in the README is that my (and @felixhagspiel 's) use case would just work.

finalgrrrl avatar Aug 25 '22 17:08 finalgrrrl

maybe we could clarify the documentation? my read of Expose as its described in the README is that my use case would just work.

i agree and you can make a PR, but dont expect it to be merged (or any other), because this project is basically not maintained anymore (only dependabot commits for a long time)

hasezoey avatar Aug 26 '22 08:08 hasezoey

Class-transformer does not generate any type definition file so I do think this question has nothing do to with this library.

Closing as invalid.

diffy0712 avatar May 16 '24 20:05 diffy0712