ETS Questions

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 3 months ago.

  • Author
    Posts
  • #67311
    Profile photo of OCram85
    OCram85
    Participant

    I recently started to overwork some old modules an bring them to my current powershell knowledge.
    So I thought adding format and type files. But now I've got stuck on some unclear things:

    – Is it useful to create a type file for each PSCustomObject I added add new Type name?
    — I thought the ETS is supposed to customize existing types and not documenting new ones
    — What's the benefit I get form this step? –

    – How to define the type with empty values?
    — It seems like the `NoteProperty` tag with `Name` and 'Value' pair doesn't allow empty values.

    – Isn't it supposed to use the defined Type now like a class with `New-Obect -TypeName -Property @{...}` ?
    — Because I can use the type in advanced function with `[OutputType('Sparc.whoami')]` but not as parameter type or to create a new object instance.

    To prevent confusion I give you some code examples of mine:

    1. Let's assume I've created a new PSCustomObject with some `NoteProperties` and finish this step changing the type name with:
    `See ObjectCreation.ps1`

    2. Now I tried to create a new format file called: `Sparc.Type.ps1xml` will will be loaded with the module.
    `See Sparc.Type.ps1xml`

    Here is my format file `Sparc.Whoami.Format.ps1xml` to complete this example:
    `See Sparc.Whoami.format.ps1xml`

    I hope this question and started discussion helps not just me. It's hard to find detailed information about this in the docs and other sides ^^

    Thank you again for your helping advice!

  • #67348
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Let's see.

    – Is it useful to create a type file for each PSCustomObject I added add new Type name?
    Well, "useful" is up to you. I create type files when I _need_ to, and it's because I need to add a member that I can't easily add otherwise. But in most cases, when I'm creating an object, I can add whatever members I want right there, so a type extension isn't really needed all the time.

    — I thought the ETS is supposed to customize existing types and not documenting new ones
    It has nothing to do with "documenting." And yes, its main use is to extend objects that already exist, that you didn't design, and that you can't easily extend otherwise.

    – How to define the type with empty values?
    You don't use the ETS to "define a type."

    — It seems like the `NoteProperty` tag with `Name` and 'Value' pair doesn't allow empty values.
    A "NoteProperty" isn't a "tag," nor does it take "pairs." A NoteProperty is a static property, and no, you cannot create one with empty values. The type system isn't about "documenting" or "defining" types, which it seems is what you're trying to do.

    – Isn't it supposed to use the defined Type now like a class with `New-Obect -TypeName -Property @{...}` ?
    No. A type file does NOT define a type in the way that a class does.

    — Because I can use the type in advanced function with `[OutputType('Sparc.whoami')]` but not as parameter type or to create a new object instance.
    That's correct. Actually, "OutputType" will take whatever you give it, so you're not really "using" a type.

You must be logged in to reply to this topic.