question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] allow use `as const` + `type` or `interface`

See original GitHub issue

Search Terms

Suggestion

Use Cases

allow use as const + type or interface

so we can make sure as const is follow type or interface

Examples

check as const output is follow IVueCliPrompt[]

.ts

interface IVueCliPrompt
{
	name: string,
	type: 'confirm' | string,
	message: string,
	default: unknown
}

const prompts = [
	{
		name: 'replaceFiles',
		type: 'confirm',
		message: 'Replace current files with preset files?',
		default: false
	},
] as const IVueCliPrompt[];

export = prompts

.d.ts

declare const prompts: readonly [{
    readonly name: "replaceFiles";
    readonly type: "confirm";
    readonly message: "Replace current files with preset files?";
    readonly default: false;
}];
export = prompts;

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:24
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
greg-hornby-roamcommented, Apr 24, 2019

I believe he wants to use the as const feature while still type checking that the structure matches an interface. A workaround I’d use for something like that would be

interface ITest {
  a: number;
  b: string;
}

let foo = {
  a: 5,
  b: "Hello"
} as const;

<ITest>foo; //a useless javascript line, but checks the var `foo` is assignable to `ITest`
3reactions
AnyhowStepcommented, Jul 17, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

const assertions are the killer new TypeScript feature
The const keyword ensures that no reassignment to the variable can happen and a strict type of only that literal is guaranteed. But...
Read more >
Typescript type check in switch statement, feature request or ...
I could solve this with: const newStringObj: AnyStringObj = { text: f1(stringObj) as string };. My question ...
Read more >
Documentation - TypeScript 3.4
Notice the above needed no type annotations. The const assertion allowed TypeScript to take the most specific type of the expression. This can...
Read more >
Effective Go - The Go Programming Language
Values; Interfaces and other types: Interfaces: Conversions: Interface ... With Go we take an unusual approach and let the machine take care of...
Read more >
What's new in C# 11 - C# Guide | Microsoft Learn
Get an overview of the new features coming in C# 11. ... This change allows types that implement generic math interfaces to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found