Skip to content

Add types for vue-server-renderer (fix #5772) #5775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2017

Conversation

ktsn
Copy link
Member

@ktsn ktsn commented May 28, 2017

What kind of change does this PR introduce? (check at least one)

  • Other, please describe: Add type declarations

Does this PR introduce a breaking change? (check one)

  • No

The PR fulfills these requirements:

Other information:
Added the type declaration file of vue-server-renderer into its package directory. Also added a brief test code of the declaration.


export declare function createRenderer(options?: RendererOptions): Renderer;

export declare function createBundleRenderer(bundle: any, options?: BundleRendererOptions): BundleRenderer;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://ssr.vuejs.org/en/api.html#createbundlerendererbundle-options

IMHO a bundle is either a string or an object.

interface RendererOptions {
template?: string;
inject?: boolean;
shouldPreload?: (file: string, type: string) => boolean;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can even refine type to a precise list of file type listed here.

Might be an overkill.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but not sure how benefit we can acquire by declaring it as string literal types. If the benefit is trivial, we can leave it as string for the simplicity of declarations?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, spec change and implementation oddity will be more problematic.

shouldPreload?: (file: string, type: string) => boolean;
cache?: RenderCache;
directives?: {
[key: string]: Vue.DirectiveOptions | Vue.DirectiveFunction

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note server side directive will have different implementation. http://ssr.vuejs.org/en/api.html#directives

dirRenderer(node, dirs[i])

IMHO direcitives will be

 [key: string]: (vnode: VNode, dir: VNodeDirective) => void

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I totally didn't know about that, thanks 😅

},

directives: {
example (vnode, directiveMeta) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vnode is inferred as HTMLElement


export declare function createBundleRenderer(bundle: any, options?: BundleRendererOptions): BundleRenderer;

type RenderCallback = (err: Error, html: string) => void;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err is nullable, if user turns on strictNullChecks TS will wrongly report if (err === null) {} as unreachable error.
(edit: strangely it does not report in 2.3.3)

done(null, result)

While html is also nullable, I think it is more desirable to declare it as string since users should use it after checking err. Declaring html as nullable will force users to unnecessarily recheck because TS cannot relate two variables in flow control :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While html is also nullable, I think it is more desirable to declare it as string

I agree. html should be string for convenience.

}

interface BundleRendererOptions extends RendererOptions {
clientManifest?: any;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update this option to object too.

@yyx990803 yyx990803 merged commit e6de9a5 into vuejs:dev May 28, 2017
@yyx990803
Copy link
Member

👍 so fast :)

@ktsn ktsn deleted the types-server-renderer branch May 28, 2017 12:34
@cdekok
Copy link

cdekok commented May 28, 2017

wow very fast fix 10x 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants