Skip to content

unknown inference in optional chaining ? #45378

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

Closed
5 tasks done
eczn opened this issue Aug 9, 2021 · 2 comments
Closed
5 tasks done

unknown inference in optional chaining ? #45378

eczn opened this issue Aug 9, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@eczn
Copy link

eczn commented Aug 9, 2021

Suggestion

interface Student { name: string };

// current: Error thrown when using i?.name because ts don't know what the keyof i will to be
const isStudent = (i: unknown): i is Student => typeof i?.name === 'string';

but in fact, optional chaining is designed for safety get the value of var, so an optional chaining for unknown types should be unknown too:

declare const i1: unknown;

const i2 = i1?.aaa; // optional chaining for unknown i1
type T = typeof i2; // T should be unknown too

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

One: for type predicate, avoiding the usage of any

interface File { fileId: string; }
interface Folder { folderId: string; }
const isFile = (i: unknown): i is File => typeof i?.fileId === 'string';
const isFolder = (i: unknown): i is Folder => typeof i?.folderId === 'string';

// better than any:
const isFile = (i: any): i is File => typeof i?.fileId === 'string';

Two: a better unknown types processing experience

const getRawUser = (id: string): Promise<unknown> => fetch(`/api/user/${id}`).then(d => d.json());

const eczn = await getRawUser(`eczn`);
const maybeAvatar = eczn?.profile?.avatar; // <<<< unknown typed maybeAvatar
// narrowing for `maybeAvatar: unknown`
if (typeof maybeAvatar === 'string') <img src={maybeAvatar} />

💻 Use Cases

@RyanCavanaugh
Copy link
Member

Duplicate #37700

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 9, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants