Giter Site home page Giter Site logo

Comments (14)

daKmoR avatar daKmoR commented on July 21, 2024 2

For documentation purposes, it would be good enough to keep types more as "type hints". e.g. to give an idea of what it should be... it does not need to be a "real" type.

Examples:

  • String (e.g. foo, bar, ...)
  • Number (e.g. 1, 4342, ...)
  • Color (e.g. #fff, orange, rgb(255,0,0), ...)
  • Css Length (e.g. 10px, 10rem, ...
  • Array (does NOT type content)
  • Object (does NOT type content)

REAL type support I would consider out of scope and should be handled separately by searching for *.d.ts files.

So many tools will be fine with only custome-elements.json. e.g. documentation, playgrounds, catalog, ...

Some tools will need to "gather" more data to also give type safety. e.g. linters, editor plugins, ...

from custom-elements-manifest.

daKmoR avatar daKmoR commented on July 21, 2024 1

Do you have an example of what you want to show in docs?

right now I have it sort of like that

Property Type Description
backSide Boolean Boolean to switch between front and back
data Array<RowData> All items to displayed with a format of { name: 'foo', age: 12, position: 'a|b|c'}

e.g. optimal for "static" documentation - so can not click on RowData but still it gives all information even in just a README.md

But yeah you need to hand write these descriptions. If you don't this information is not there

Property Type Description
backSide Boolean
data Array<RowData>

I think even if it's not perfect it's already quite a good start... I just fear if we try to incorporate a full typing system into this... that it's a very deep rabbit hole and it will postpone any delivery "forever". Also, I can imagine that such a system might make it almost impossible to hand write custom-elements.json - which imho should be a goal.

Considering this we could have a typeHint for now which is "just" a string like Boolean, Array<RowData and "reserve" type for a real type system.

from custom-elements-manifest.

evanplaice avatar evanplaice commented on July 21, 2024 1

Types that map to TS types would be nice to have...

And types -- when provided -- should work as-is from a JS-specific perspective 1

1Even typed JS using JSDoc. TS 3.6 added the ability to generate *.d.ts typings directly from JSDoc docstrings

The problem is...

The greatest benefit of WC is that they can be consumed directly in HTML. As in, they unlock a whole new ecosystem of capabilities for users who have little-no knowledge of JS.

From a HTML-specific perspective, there are only 2 possible types of attributes string|boolean.

string

<some-element string="somestring"></some-element>

boolean

<some-element bool-attrib></some-element>

What would hugely benefit the users1

  • auto-completion for attribute names
  • inline documentation popups
  • usable w/o access to documentation

In the absence of convenience tooling...

  • can users quickly grasp how the WC is defined in HTML using custom-elements.json?

2Including Users who are less sophisticated in and/or lack JS/TS knowledge altogether


With that said, I don't see any downside to including a meta | metaType property for use in documentation, demos, etc.

{
  "version": 0,
  "tags": [
    {
      "name": "wc-markdown",
      "description": "A Markdown web component",
      "attributes": [
        {
          "name": "src",
          "description": "URL for the input markdown source",
          "type": "string",
          "required": true,
          "meta": "URL"
        }
      ]
    }
]

from custom-elements-manifest.

thepassle avatar thepassle commented on July 21, 2024 1

What about making a type declaration type,

I'd really prefer to stay as far from this as possible and avoid becoming an alternative/substitute for type declaration files.

iirc, if useSubscription is a function, that means the CEM should already list its return type, right? Function params/return types are supported by the current schema?

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

I would really like for documentation viewers to be able to show all the HTML, CSS and JS APIs, and cross-reference JS API types to their definitions so we can provide full usage. That'll require better type annotations than the norm in jsdocs. TypeScript has a well-defined type syntax, while jsdoc-style ones vary quite a bit, especially if you include Closure types.

One thing I worry about when leaving this up to TypeScript declarations is that it increases the complexity of doc viewers. You can't just pull a .json file from unpkg.com, say, you must include a full TypeScript parser.

from custom-elements-manifest.

KrisGraySFDC avatar KrisGraySFDC commented on July 21, 2024

LWC had this same issue and thats why we ended up with two sets of descriptions per web component. There's the parsed type from the metadata and the documented type.

/**
 * Specify either a Dom Element or element Id
 * @type String | HtmlElement
 */
target = "domId";

// Psuedo format
meta: {
    attributes: [
      { name: "target", type: "string" }
    ]
},
documentation: {
    attributes: [
      { name: "target", type: "String | HtmlElement" }
    ]
}

If you're using Typescript, you would just use the type from the definition files, but if not do we surface "string" or "String | HtmlElement" to the user?

I'd advocate for documenting both, otherwise I vote for typeHint = metadataType || documentationType.

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

@evanplaice types would only be for properties and methods. Attributes must be strings and instead of a type they might have some sort of enum or micro syntax as you see in the HTML spec. Supporting TypeScript type notation for the JS properties has no downside to HTML users.

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

In runem/web-component-analyzer#140 @telpalbrox is asking for better union type handling. I think instead of case-by-case making type handling better via inlining and converting TypeScript types to something somewhat ad-hoc, we'll be better off adopting a full translation of TypeScript types into the analysis output.

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

Just a note that this is still an open question, even though we've been developing a pretty extensive format that includes classes, variables, and functions in #9. It does not yet have interfaces and type aliases.

from custom-elements-manifest.

thepassle avatar thepassle commented on July 21, 2024

I wonder what this should look like exactly. As far as I can see there are three scenarios:

  • A user does not use types -> no type information available
  • A user uses jsdoc types -> some type information available
  • A user uses full typings with TS -> full type information available

[...] named as ButtonSize and output a reference to a definition of the ButtonSize type.

if I understand correctly, we could point to the typing file for the ButtonSize type

Considering those things, we could have an optional TypeDoc maybe? e.g.:

export interface TypeDoc {
  /** JSdoc type or TS type */
  type?: string;
  /** Reference to the definition of the type in case of TS */
  declaration?: string;
}

export interface FieldDoc {
  // ...
  type?: TypeDoc;
}

Similarly for methods we could have an optional InterfaceDoc

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

I went looking for API Extractor's representation of types it's kind of interesting. There's a balance between a string-only representation that would make cross-referrences difficult-to-impossible to build, and a full tree representation of the type expression, which could be cumbersome and have to keep up with changes in type syntax.

What I think we could do that is similar to API extractor is treat the type as a string, but have an optional list of references that index into the type string.

ex:

export interface TypeDoc {
  /** JSdoc type or TS type */
  type: string;

  references?: Array<TypeReferenceDoc>;
}

export interface TypeReference extends Reference {
  // start and end must both be present or not present.
  // if they're present, they are indices into the `type` string
  // if they are missing, the entire type string is the symbol
  // referenced and the name should match the type string
  start?: number;
  end?: number;
}

export interface FieldDoc {
  // ...
  type?: TypeDoc;
}

I think this should work equally well for JSDoc types or any type system really, because all we care about is the string representation and references which tools understanding specific type annotations will generate.

wdyt? cc @rictic @runem

from custom-elements-manifest.

bennypowers avatar bennypowers commented on July 21, 2024

What about making a type declaration type, which you'd reference by package and module like the rest?
it could have optional params and properties, like this partial:

{
  "modules": [{
    "path": "./types.ts",
    "exports": [{
      "name": "FooOptions",
      "kind": "type",
      "properties": [{
        "name": "foo",
        "type": "Array<D>"
      }],
      "parameters": [{
        "type": "string|number",
        "name": "D"
      }],
    }]
  }]
}

Guiding example:
Screen shot showing exported function parameters table with links to parameter types

We can generate this page from the current schema, with links to internal types,
But we can't generate a table of type params or properties from the current schema, so those links would have nowhere to go to, save bespoke content

from custom-elements-manifest.

bennypowers avatar bennypowers commented on July 21, 2024

There's a markdown summary on parameters that I can just manually add a table to, If we added the same to return that would cover a lot of my case for now. The author would still need to manually link types inside, but I'm ready to do that with a postprocessor for now. WDYT @justinfagnani

See #32

from custom-elements-manifest.

justinfagnani avatar justinfagnani commented on July 21, 2024

Closing this issue since I went with a strategy inspired by API Extractor - to have type references be an object with a text form of the type and optionally a number of string-offset-based references to other types.

I do think that we may soon want some kind of "Type" type to describe the properties of a type used in other type references. I hope we can stay close to the type-system agnostic approach we have with type references. I'm thinking we can flatten properties and not represent any type composition or type algebra - so it'd be very much like the classes we have now, but just as an interface. I'll open a separate issue for that.

from custom-elements-manifest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.