Skip to content

TypeScript view #11

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

Open
Rich-Harris opened this issue May 9, 2022 · 3 comments
Open

TypeScript view #11

Rich-Harris opened this issue May 9, 2022 · 3 comments

Comments

@Rich-Harris
Copy link
Member

would be neat if we could opt into TypeScript, both as .ts and lang="ts", and opt into JSDoc otherwise

@stephane-vanraes
Copy link
Contributor

One option would be a toggle in the sidebar to change between TS and JS

image

But I think the main issue with this is that it requires to write all content in both typescript and javascript, which could be a lot of duplication/extra work.

What about a section Part 5: TypeScript ?
Here we can then put the most common pitfalls surrounding typescript:

  • Adding TypeScript (running the setup script ? / lang=ts)
  • app.d.ts what is how, how to add your own types
  • other things...

@Rich-Harris
Copy link
Member Author

it requires to write all content in both typescript and javascript

Not necessarily. We have the same problem for create-svelte, and we solve it like this:

<script lang="ts">
	import { enhance } from '$lib/form';
	import { scale } from 'svelte/transition';
	import { flip } from 'svelte/animate';

	/**
	 * @typedef {{
	 *   uid: string;
	 *   created_at: Date;
	 *   text: string;
	 *   done: boolean;
	 *   pending_delete: boolean;
	 * }} Todo
	 */

	type Todo = {
		uid: string;
		created_at: Date;
		text: string;
		done: boolean;
		pending_delete: boolean;
	};

	/** @type {Todo[]} */
	export let todos: Todo[];
</script>
import { api } from './_api';
import type { RequestHandler } from './__types';

/** @type {import('./__types').RequestHandler} */
export const get: RequestHandler = async ({ locals }) => {
	// ...
}

In other words we write TypeScript and annotate it with redundant JS, then we have the option of keeping the TS and removing the JSDoc, keeping the JSDoc and removing the TS, or removing both. It's a bit more work than just writing type-less code, but it's not twice as much work, and there's little danger of things falling out of sync. We could do the same here.

Design-wise I feel like it needs to be a top-level option (maybe in the menu) since it should apply to code snippets in the sidebar as well.

@gtm-nayan
Copy link
Contributor

I have a feeling comparing contents for the solve button would end up being kinda painful,

maybe it won't be with fetching the transformed files from vite for comparison.

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

No branches or pull requests

3 participants